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); 

    }) 

그러나 이것은 작동하지 않습니다. 난 그냥 하나의 속성에 감시자를 설정하려고하지만 경우에 잘 작동하고 코드는

가 가

사람이 세부 사항이 설명 할 수 왜이 문제가 일어나고

.directive('myDirective', function() { 
     return { 
      restrict: 'A', 
      scope: true, 
      link: function ($scope, element, attrs) { 
       $scope.$watch(attrs.attrOne, 
       function (newVal) { 
       console.log(newVal); 
       }); 
     }); 
아래와 같습니다?

+0

@georgeawg의 쿵하는 소리에 왜 ATTR-하나 X를 통과 않았다 = "{{X}}"왜 안 ATTR-하나 = "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