Skip to content Skip to sidebar Skip to footer

Angularjs And Structural/behavbiour Coupling?

I'm trying to understand how does AngularJs is not violating best practices : Looking at
...
It has those benefits : Behave the s

Solution 1:

The main difference between using ng-click vs another javascript click handler is that any event handling outside of angular scope requires calling scope.$apply() in order to tell angular a change was made and to run a digest for that scope.

When you use an ng directive for event handling, the ng directives will take care of running the new digest for you.

Consider these two directives that perform identical tasks:

HTML

<buttononeng-click="doSomething()">Update</button><buttontwo>Update</button>

JS

app.directive('one', function() {
  returnfunction(scope) {
      scope.doSomething = function() {
        scope.text_1 = "New Text"
      }       
  }
});

 app.directive('two', function() {
  returnfunction(scope, elem) {
        elem.on('click',function(){
          scope.$apply(function(){/* must tell angular we're making a change*/
            scope.text_2='New Text'
          });
        });
  }
});

First requires more markup, but is easier to test, second requires additional code to notify angular of changes

DEMO

Post a Comment for "Angularjs And Structural/behavbiour Coupling?"