Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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
基于if语句添加到Javascript数组_Javascript_Arrays_Angularjs_Validation - Fatal编程技术网

基于if语句添加到Javascript数组

基于if语句添加到Javascript数组,javascript,arrays,angularjs,validation,Javascript,Arrays,Angularjs,Validation,我需要根据if语句的结果向数组中添加错误消息。下面的代码仅当我输入了应该触发这两个错误的数据时才会返回第二条错误消息。我知道.push()不仅仅是添加到以前的数组中,他们唯一需要修复的方法是生成for循环。我想我只是缺少了一些简单的函数 $scope.error = {}; if ($password == null || $vPassword == null || $email == null || $vEmail == null) { console.log(

我需要根据if语句的结果向数组中添加错误消息。下面的代码仅当我输入了应该触发这两个错误的数据时才会返回第二条错误消息。我知道.push()不仅仅是添加到以前的数组中,他们唯一需要修复的方法是生成for循环。我想我只是缺少了一些简单的函数

    $scope.error = {};
    if ($password == null || $vPassword == null || $email == null || $vEmail == null) {
        console.log('All fields must be filled in');
    } else {
        if ($scope.verifyPassword !== $scope.password) {
            $scope.error[scope.error.length()] = 'Your passwords must match...';
        }
        if ($scope.verifyEmail !== $scope.email) {
            $scope.error.push = ['Your email addresses must match...'];
        }
        console.log($scope.error);
    }
我认为这段代码很容易解释。如果你需要更多信息,请告诉我

谢谢


编辑:我不确定是什么代码产生了我解释的错误。很抱歉上面的代码是我为了让它正常工作而搞砸的垃圾代码。很明显,大部分都没有意义。被接受的答案是我最初认为会产生这种错误的东西,但显然它是有效的。很抱歉这篇蹩脚的文章。

push
是一个函数,应该使用
()
调用它

此外,不能在非数组的对象上调用
push
。必须将
$scope.error
实例化为
数组

$scope.error = [];

push
是一个函数,应该使用
()
调用

此外,不能在非数组的对象上调用
push
。必须将
$scope.error
实例化为
数组

$scope.error = [];

push
是一个函数,应该使用
()
调用

此外,不能在非数组的对象上调用
push
。必须将
$scope.error
实例化为
数组

$scope.error = [];

push
是一个函数,应该使用
()
调用

此外,不能在非数组的对象上调用
push
。必须将
$scope.error
实例化为
数组

$scope.error = [];

您需要将
$scope.error
更改为数组,而不是对象,然后
.push()
将正常工作

$scope.error=[];
$scope.error.push(“您的密码必须匹配…”);
log($scope.error);
产生

["Your passwords must match..."]

您需要将
$scope.error
更改为数组,而不是对象,然后
.push()
将正常工作

$scope.error=[];
$scope.error.push(“您的密码必须匹配…”);
log($scope.error);
产生

["Your passwords must match..."]

您需要将
$scope.error
更改为数组,而不是对象,然后
.push()
将正常工作

$scope.error=[];
$scope.error.push(“您的密码必须匹配…”);
log($scope.error);
产生

["Your passwords must match..."]

您需要将
$scope.error
更改为数组,而不是对象,然后
.push()
将正常工作

$scope.error=[];
$scope.error.push(“您的密码必须匹配…”);
log($scope.error);
产生

["Your passwords must match..."]

对于数组:
$scope.error=[]
;然后根据数组的需要推送(“字符串”):
$scope.error=[]
;然后根据数组的需要推送(“字符串”):
$scope.error=[]
;然后根据数组的需要推送(“字符串”):
$scope.error=[]
;然后根据需要推一下(“字符串”)。我并不像我的问题让我看起来那么笨。阅读我对问题的编辑。谢谢你!它起作用了!我不像我的问题让我看起来那么傻。阅读我对问题的编辑。谢谢你!它起作用了!我不像我的问题让我看起来那么傻。阅读我对问题的编辑。谢谢你!它起作用了!我不像我的问题让我看起来那么傻。阅读我对问题的编辑。谢谢你!它起作用了!