角度范围。$ watch触发两次

时间:2017-05-24 16:50:57

标签: javascript angularjs

我有以下代码:

html模板:

<button class=""  ng-click="item_popup.toggle()"/>

在一个指令中:

app.directive('itemPopup', ['$sce', '$timeout', function($sce, $timeout){
    return {
        restrict: 'E',
        replace: true,
        templateUrl: $sce.trustAsResourceUrl('/'),
        link: function(scope, el, attrs) {

    toggle = function(){
        item.toggle = scope.items.checked == 0 ? 1 : 0
    }
}]);

和app:

app.directive('item', ['$filter','$timeout', function($filter,$timeout){
        scope.$watch('item.toggle', function(newValue,oldValue){
            if(newValue!=undefined && oldValue!=undefined && oldValue != newValue){
                // Do something 
            }
      });
}]);

这里的问题是每次调用toggle函数,范围。$ watch被触发两次。

我试着检查一些其他答案,例如:

Angular expressions in $watch trigger twice

$rootScope.$watch triggered twice

但他们没有给出答案。

0 个答案:

没有答案
相关问题