Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $watch仅在特定事件后查看属性_Javascript_Angularjs_Angularjs Scope - Fatal编程技术网

Javascript $watch仅在特定事件后查看属性

Javascript $watch仅在特定事件后查看属性,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我正在学习angular,我正在尝试构建一个表单: 1.on-blur检查属性值是否唯一 2.如果该值不是唯一的,则它将开始监视该值,并将表单上的错误作为用户类型给出或删除 我个人认为这种方式更方便用户。键入时不会出现错误,但一旦出现错误,在更正时,错误就会消失 这是我的代码: $scope.checkUniqueValue = function(){ var result = true; for(var i=0; i < $scope.users.length; i+

我正在学习angular,我正在尝试构建一个表单:

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;
};
$scope.checkUniqueValue=function(){
var结果=真;
对于(变量i=0;i<$scope.users.length;i++){
if($scope.users[i].name==$scope.userName){
结果=假;
打破
}
}
如果(结果){
$scope.error=null;
}否则{
$scope.error=“名称不唯一”;
$scope.$watch('userName',function(){
$scope.checkUniqueValue();
});
}
返回结果;
};
它的工作方式很好。然而,当我在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
Uncaught错误:[$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%…5D angular.js:36
错误:[$rootScope:infdig]http://errors.angularjs.org/1.2.21/$rootScope/infdig?p0=10&p1=%5B%5B%22用户…me%3B%20newVal%3A%20%5C%22asghar%5C%22%3B%20oldVal%3A%20未定义%22%5D%5D
错误(本机)
在http://localhost:8080/bower_components/angular/angular.min.js:6:450
售价k$digest(http://localhost:8080/bower_components/angular/angular.min.js:110:66)
以千元申请(http://localhost:8080/bower_components/angular/angular.min.js:112:173)
在HTMLInputElement.l(http://localhost:8080/bower_components/angular/angular.min.js:136:370)
在HTMLInputElement.n.event.dispatch(http://localhost:8080/bower_components/jquery/dist/jquery.min.js:3:6404)
在HTMLInputElement.r.handle处(http://localhost:8080/bower_components/jquery/dist/jquery.min.js:3:3179)js:10023
我想知道是否有人知道这些错误意味着什么,我在这里做错了什么。因为代码可以工作,所以我很难找到导致它的问题

我还想知道是否有更好的方法将$watch嵌套在if语句中,或者有更好的方法不使用$watch来实现这一点


谢谢

您得到的错误意味着
无限$digest循环
()

基本上是:

    $scope.$watch( 'userName', function() {
        $scope.checkUniqueValue();
    });
将始终触发摘要循环,并且无论该值是否已更改,监视程序始终使用新值获取初始化调用,因此,一旦达到上面的代码

        $scope.checkUniqueValue();

将始终被调用,并且由于没有任何更改,您将定义另一个手表,它将再次调用
checkUniqueValue()
,这将设置另一个手表。。。循环将永远持续。

您应该将$watch移出函数checkUniqueValue,因为它不需要驻留在那里

是您的朋友。是否有限制ng更改仅在您获得模糊错误后才开始工作的方法?但是如果模糊没有错误,ng更改仍然处于停用状态?为什么您希望在提交表单之前显示错误总是很好。您可以做的一件事是对输入的文本数量设置一个基本限制。i、 例如,如果用户键入的字符数超过3个,则仅显示错误