我试图找出在指令中更新模型时如何收听。
eventEditor.directive('myAmount',function(){
return {
restrict: 'A',
link: function(scope, elem, attrs) {
scope.$watch(attr['ngModel'], function (v) {
console.log('value changed, new value is: ' + v);
});
}
}
}
};
});
该指令在 ng-repeat 中被调用为
<div ng-repeat="ticket in tickets">
<input my-amount ng-model="ticket.price"></input>
</div>
很高兴得到任何帮助。我不明白 ng-repeat 中的 scope 属性是怎样的。
谢谢。
最佳答案
http://jsbin.com/mihupo/1/editattrs
相反 attr
app.directive('myAmount',function(){
return {
restrict: 'A',
link: function(scope, elem, attrs) {
scope.$watch(attrs['ngModel'], function (v) {
console.log('value changed, new value is: ' + v);
});
}
} ;
}
);
关于Angularjs:听指令中的模型变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25878189/