0

ディレクティブの複数の属性で共通ウォッチャーを設定しようとしています。私のコードは次のとおりですウォッチャーは、ディレクティブの複数の属性を角度で設定する

.directive('myDirective', function() { 
     return { 
      restrict: 'A', 
      scope: true, 
      link: function ($scope, element, attrs) { 
       $scope.$watch(function() { 
        return [attrs.attrOne]; 
       }, function (newVal) { 
        console.log(newVal); 
       }, true); 

    }) 

しかし、これはうまくいきませんが、私はすでにオブジェクトの等価性の3番目のパラメータをtrueに設定しています。しかし、私はちょうど単一の属性にウォッチャーを設定しようとすると、それが正常に動作して

.directive('myDirective', function() { 
     return { 
      restrict: 'A', 
      scope: true, 
      link: function ($scope, element, attrs) { 
       $scope.$watch(attrs.attrOne, 
       function (newVal) { 
       console.log(newVal); 
       }); 
     }); 

以下のようにコードがある誰もが、なぜこの行動が起こっている、詳細にこれを説明することはできますか?

+0

@georgeawg、plunkになぜあなたはATTR-1としてXを通過しなかった= "{{X}}" なぜないのattr-1 = "X"、ここでは中括弧 – madhur

答えて

0

AngularJS 1.3からは、一連の式を観察するための$ watchGroupという新しいメソッドがあります。

$scope.foo = 'foo'; 
$scope.bar = 'bar'; 

$scope.$watchGroup(['foo', 'bar'], function(newValues, oldValues, scope) { 
    // newValues array contains the current values of the watch expressions 
    // with the indexes matching those of the watchExpression array 
    // i.e. 
    // newValues[0] -> $scope.foo 
    // and 
    // newValues[1] -> $scope.bar 
}); 
+0

問題の具体的な理由があります私は1.0.8になっているので、これは私には利用できません – madhur

関連する問題