在离子中进行基本验证

时间:2016-09-28 07:04:51

标签: angularjs ionic-framework

嗨,我正试着离子和angularjs。我有一个处理电话列表的示例代码。代码的工作是将电话号码添加到列表中并删除所选的号码,并且当输入号码时,应该检查号码是否存在,如果号码不存在则应该添加它。我在这里面临的问题是检查列表中是否存在电话号码。

link

 $scope.phonelist = [];
    $scope.add = function() {
      $scope.phonelist.push($scope.phone);
      $scope.phone = {};
    }
    $scope.removeSelected = function() {
      var i = $scope.phonelist.length;
      // reversed loop because you change the array
      while (i--) {
        var phone = $scope.phonelist[i];
        if (phone.checked) {
          $scope.phonelist.splice(i, 1);
        }

1 个答案:

答案 0 :(得分:1)

$scope.phonelist = [];
var added;
$scope.add = function() {
    added = false;
    for (var j=0;j<$scope.phonelist.length;j++) {
        if (angular.equals($scope.phonelist[j], $scope.phone)) {
            added=true;
        }
    }
    if (!added) {
        $scope.phonelist.push($scope.phone);
        $scope.phone = {};
    } else {
        alert('error');
    } 
}

像这样更新你的控制器以增加功能。感谢