我正在學習角度,我正試圖構建一個表單:
1.on-blur檢查屬性值是否唯一。
2.如果該值不是唯一的,那麼它會開始觀察值並在用戶類型上給出或刪除表單上的錯誤。
我個人認爲這種方式更加用戶友好。輸入時不會出現錯誤,但一旦出現錯誤,當您糾正錯誤時,錯誤就會消失。
這是我的代碼:
$scope.checkUniqueValue = function(){
var result = true;
for(var i=0; i < $scope.users.length; i++){
if($scope.users[i].name === $scope.userName){
result = false;
break;
}
}
if(result){
$scope.error = null;
}else{
$scope.error = "name is not unique";
$scope.$watch('userName', function() {
$scope.checkUniqueValue();
});
}
return result;
};
它工作正常的方式應該。然而,當我在Chrome中打開開發人員工具,我看到這一切的錯誤:
Uncaught Error: [$rootScope:infdig] http://errors.angularjs.org/1.2.21/$rootScope/infdig?p0=10&p1=%5B%5B%22user…ned%22%5D%2C%5B%22userName%3B%20newVal%3A%20%5C%22akbar%5C%22%3B%20oldVal%...<omitted>...5D angular.js:36
Error: [$rootScope:infdig] http://errors.angularjs.org/1.2.21/$rootScope/infdig?p0=10&p1=%5B%5B%22user…me%3B%20newVal%3A%20%5C%22asghar%5C%22%3B%20oldVal%3A%20undefined%22%5D%5D
at Error (native)
at http://localhost:8080/bower_components/angular/angular.min.js:6:450
at k.$digest (http://localhost:8080/bower_components/angular/angular.min.js:110:66)
at k.$apply (http://localhost:8080/bower_components/angular/angular.min.js:112:173)
at HTMLInputElement.l (http://localhost:8080/bower_components/angular/angular.min.js:136:370)
at HTMLInputElement.n.event.dispatch (http://localhost:8080/bower_components/jquery/dist/jquery.min.js:3:6404)
at HTMLInputElement.r.handle (http://localhost:8080/bower_components/jquery/dist/jquery.min.js:3:3179) angular.js:10023
我想知道是否有人知道這些錯誤是什麼意思,什麼我做錯了here.since代碼工作,這對我來說是困難找出導致它的問題。
另外我想知道是否有一個更好的方法來嵌套$ watch內if語句或有更好的方法來處理這個沒有使用$ watch。
謝謝
[ngChange](https://docs.angularjs.org/api/ng/directive/ngChange)是你的朋友。 – 2014-08-27 22:16:01
是否有限制ng-change只有在模糊上出現錯誤後纔開始工作?但如果在模糊上沒有錯誤,ng-change仍然被禁用? – 2014-08-27 22:21:38
爲什麼你會希望事先顯示錯誤然後等待表單提交。你可以做的一件事是設置輸入文本數量的基本限制。也就是說,如果用戶鍵入了超過3個字符,則只會顯示錯誤 – 2014-08-27 22:24:56