I’ve been struggling with understanding when and how to use jQuery inside an AngularJS directive and have the data binding work as expected. I’ve got a good example I put together that shows how to do this as well as shows when you need to use the scope.$apply().
You can see from the below example that in the directive, I am using AngularJS. Because our DOM is being modified outside of what AngularJS can see, it does not have a watch assigned to it and therefore in the AngularJS digest loop, the scope.cntjQuery does not get updated. As we know, the $scope contains the “truth” of this application but if the digest loop is not run then the truth never makes it to the browser. Calling scope.$apply() causes the digest loop to be run.
Your new post is loading...