javascript - AngularJS: calling $watchGroup from scope -


how can call $watctgroup form link function of directive. i've got 2 simple scope elements (integer) , i'd watch them in pair. suppose $watchgroup more effective use $watch('[element1, element2'],..).

like this:

scope.element1=1; scope.element2=2; scope.$watchgroup(['element1','element2'], function(){/* code here */}); 

update:

bare in mind $watchgroup started being available in angularjs 1.3, if using previous version won't work.

example of how use $watchgroup in directive:

angular.module ('testapp' , []) .directive ('testdirective', function (){     return{         restrict:'a',         replace:true,         template: '<div ng-click="inc()">{{element1}} <br/> {{element2}}</div>',         link: function(scope, element, attrs){             scope.element1=0;             scope.element2=0;             scope.inc = function(){scope.element1++;scope.element2--};             scope.$watchgroup(['element1', 'element2'], function(){                 console.log('something changed!');             });         }     }  }); 

plunker


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -