javascript - $watch not being fired -


i using code angularjs implementation of scrollspy (original article here) have ran problem when navigation dynamically created, work when navigation statically created.

so have scrollspy directive watches list of spies. spies list list of navigational elements should highlight user scrolls through page. spies added via addspy method in scrollspy controller so

controller: function ($scope) {     $scope.spies = [];     return this.addspy = function (spyobj) {         return $scope.spies.push(spyobj);     }; }, 

the addspy function gets called, when add spies dynamically $watch list never gets fired, fired when navigational items statically created.

link: function (scope, elem, attrs) {     scope.$watch('spies', function (spies) {         // never called when spies added dynamically,          // though spies added $scope.spies object in controller!     } 

can me understand why $watch isnt being fired? tried adding $scope.$apply said inside digest cycle.

scope.$watch('spies', function (spies) {     // never called when spies added dynamically,      // though spies added $scope.spies object in controller! }, true); 

you have tack on ,true @ end you're not changing reference, updating it.


Comments