2015-05-18 2 views
0

Я немного застрял в директиве, которая добавляет атрибуты и перекомпилирует элемент.AngularJs, когда директива компилирует новые атрибуты, события не запускаются, если есть область

Если у меня есть область действия директивы ng-change больше не запускается (без нее она работает). Я основано мое испытание на этом answer

HTML-

<div ng-app="myApp"> 
    <div ng-controller='testController'> 
     <div ng-repeat="field in fields"> 
      <input type="text" ng-model="ngModel[field.fieldName]" property="{{formParams.getProperties(field.fieldName)}}" update-attr ng-change="test()" /> 
     </div> 
    </div> 
</div> 

директива:

angular.module('myApp', []) 
    .controller('testController', function ($scope) { 
     $scope.properties = { 
      "something": { 
       "style": "float:left;" 
      }, 
       "something2": { 
       "style": "float:right;" 
      } 
     }; 

     $scope.ngModel = {}; 
     $scope.fields = [{ 
      fieldName: 'something' 
     }, { 
      fieldName: 'something2' 
     }]; 
     $scope.test = function() { 
      alert('i dont get triggered'); 
     }; 
     $scope.formParams = { 
      getProperties: function (fieldName) { 
       return $scope.properties[fieldName]; 
      } 
     }; 
    }) 
    .directive('updateAttr', function ($compile) { 
     return { 
      restrict: 'A', 
      replace: true, 
      terminate: true, 
      scope: { 
       ngModel : '=' 
      }, 
      link: function (scope, elem, attrs) { 
       if (angular.isDefined(attrs['property']) && attrs['property'].lenght != 0) { 
        var json = JSON.parse(attrs['property']); 
        angular.forEach(json, function (value, key) { 
         elem.attr(key, value); 
        }); 
        elem.removeAttr('property'); 
        var $e = $compile(elem[0].outerHTML)(scope); 
        elem.replaceWith($e); 
       } 
      } 
     }; 
    }); 

Здесь вилка скрипку, чтобы проверить с областью на директиве: fiddle

Есть ли у вас предложения ?

ответ

0

Я нашел почему-нг изменение не вызовет, так что я разделяю ответ:

Когда мы добавляем атрибут области действия на директивы, новый размах создается. Поэтому мы должны использовать $ scope. $ Parent для компиляции. Я обновил скрипку с исправлением.