【发布时间】:2015-04-17 19:06:11
【问题描述】:
我是 Angular js 的新手,所以请有人帮帮我。我在这里有我的模板:
<form ng-model="signup" form-valid>
<input type="text" name="username" ng-model="signup.username">{{signup}}
</form>
我的指令是这样的:
app.directive("formValid",function(){
return {
restrict:"A",
require:"ngModel",
link:function(scope,element,attrs){
scope.$watch(attrs.ngModel,function(newValue){
if(newValue){
console.log(newValue);
}
});
}
}});
当我在文本框中输入一些值时,模型正在改变,因此应该触发“$watch”。但是在这里,当我第一次在文本框中输入任何值时,“$watch”只会触发一次.提前致谢。
【问题讨论】:
-
您可能想查看模型的属性,而不仅仅是对象本身:
scope.$watch(attrs.ngModel, {…}, true); -
它对我有用。第三个参数“true”在做什么。以前没有“true”,它只触发一次。
-
第三个参数是看“深”与否。例如检查其相同的对象或检查对象具有所有相同的键/值
标签: angularjs