如何检查数组列表是否包含angularjs中的对象

时间:2015-11-14 09:39:22

标签: angularjs

我已经向$ scope.MultiEmailsList添加了几个邮件ID,然后我已经将单个电子邮件分配给$ scope.SingleEmail.Now我想用$ scope.MultiEmailsList列表检查$ scope.SingleEmail。

if ($scope.MultiEmailsList.contains($scope.SingleEmail))
 {
    console.log("Mail Id Already Exists");
 }
else
  {
     console.log("Mail Id Not Exists");            
  }

3 个答案:

答案 0 :(得分:1)

你可以写一个非常简单的函数:

function existsInArray( arr, item ) {

    for( var i = 0; i < arr.length; i++ ) 
        if( arr[ i ] === item ) return true;

    return false; 

}

或使用Underscore.js内置:_.indexOf( arr, item );功能。 :)

答案 1 :(得分:0)

它将解决您的问题:

if ($scope.Email.indexOf($scope.user.Email) !== -1)
            {
                console.log("Mail Id Already Exists");
            }
            else
            {
                $http({
                    method: 'POST',
                    url: 'index.php/Welcome/Signup',
                    data: $scope.user,
                    headers: {'Content-Type': 'application/x-www-form-urlencode'}
                }).success(function (data) {
                    $scope.Enquiry = data;
                    console.log(data);
                    $location.path('/EnquiryResponse');
                });
            }

答案 2 :(得分:0)

$scope.MultiEmailsList = ["nitin@gmail.com","test@gmail.com"];
$scope.SingleEmail = "nitin@gmail.com";


if(($scope.MultiEmailsList).indexOf($scope.SingleEmail)!= -1){
    console.log("value exists");
}else{
   console.log("value not exists");
}

请检查这可能对您有帮助..