[英]AngularJS: calling $watchGroup from scope
如何調用$ watctGroup指令的表單鏈接函數。 我有兩個簡單的范圍元素(整數),我想成對觀看。 我想$ watchGroup比使用$ watch('[element1,element2'],..)之類的東西更有效。
像這樣:
scope.element1=1;
scope.element2=2;
scope.$watchGroup(['element1','element2'], function(){/* your code here */});
切記$watchGroup
在AngularJS 1.3中開始可用,如果您使用的是以前的版本,它將無法使用。
如何在指令中使用$watchGroup
示例:
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!');
});
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.