我是新来的角的js发射只有一次,所以请有人帮我out.I有我的模板在这里:
<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);' – Blackhole
它适用于我。什么是第三个参数“真正的“在做。以前没有”真实“它只发射一次。 – Nitya
第三个参数是观察“深”还是不。例如检查它的同一对象或检查对象是否具有相同的键/值 – dogmatic69