【发布时间】:2015-12-21 01:37:12
【问题描述】:
我正在编写的指令有问题。
在指令的模板中还有另一个元素指令。
本质上,外部指令是内部的装饰器,添加更多功能..
我遇到的问题是 $pristine 和 $dirty 值没有像我预期的那样设置。
我已经修改了下面的小提琴来演示类似的场景.. (代码如下:)
HTML
<body ng-app="demo" ng-controller="DemoController">
<h3>rn-stepper demo (3/5)</h3>
Model value : {{ rating }}<br>
<hr>
<div ng-model="rating" rn-stepper></div>
</body>
JS
angular.module('demo', [])
.controller('DemoController', function($scope) {
$scope.rating = 42;
})
.directive('test', function() {
return {
restrict: 'E',
scope: {
ngModel: '=ngModel'
},
template: '<input type="text" ng-model="ngModel"></input>'
};
})
.directive('rnStepper', function() {
return {
restrict: 'AE',
scope: {
value: '=ngModel'
},
template: '<button ng-click="decrement()">-</button>' +
'<div>{{ value }}</div>' +
'<button ng-click="increment()">+</button>' +
'<test ng-model="value"></test>',
link: function(scope, iElement, iAttrs) {
scope.increment = function() {
scope.value++;
}
scope.decrement = function() {
scope.value--;
}
}
};
});
模型按预期共享,当我更改文本输入或使用滑块中的值时,绑定有效 - 但是如果我更新文本输入中的值,只有文本输入被标记为 ng-dirty - 元素指令本身与外部 div 一样保持 ng-pristine。
我不明白为什么会这样并且值没有传播到元素?这是预期的行为 - 如果是这样,我如何将 ng-dirty 等值传播到元素指令和外部 div ..
注意:我只能使用 Angular v 1.2.x,因为代码需要兼容 IE8。
提前谢谢..
【问题讨论】:
标签: angularjs nested directive