使用迭代无法验证ng重复输入值

时间:2017-08-14 12:52:14

标签: javascript angularjs

<tr ng-repeat="labour in labourdata">
        <td>{{$index+1}}</td>
        <td data-title="'Mobile No'" sortable="Mobile no">
            <input type="text" class="form-control"  name="mobile" id="mobile" ng-model="labour.mobile" value={{labour.mobile}} placeholder="Enter Aadhaar" typeahead="mobile.Mobileid for mobile in MobileArray"  ng-blur="change_mobile(mobile)" >
        </td>
        <td data-title="'NAME'" sortable="NAME">
            <input type="hidden" ng-model="labour.lid" id="lid" name="lid" />
            <input type="text" class="form-control"  id="name" ng-model="labour.name" placeholder="Name" readonly>
        </td>
        <td>
            <input type="button" ng-click="addFormField(mobile)" class="btn btn-default" value="Add " /> 
        </td>
    </tr>

这是Js代码

//Get Mobile Number
    $scope.labourdata = [{}];
    $scope.MobileArray = [];
    $http({method: 'GET',url: 'get-num.php',labourdata: { applicationId: 3 }})
    .success(function (labourdata)
    {
        $scope.MobileArray = labourdata;
    });
    // Change Mobile number
    $scope.change_mobile=function(mobile)
    {
        var j=-1;
        var mobile = "";
        for (var i = 0; i < $scope.labourdata.length; i++)
        {
            j++;
            mobile = $scope.labourdata[i]["mobile"];
        }
        $http.post('get-labour-details.php', {'selectedmobile': mobile})
        .success(function (data, status, headers, config)
        {
           var comArr = eval($scope.labourdata);
           comArr[j].lid = data[0]["LabourId"];
           comArr[j].name = data[0]["Name"];
        });
    }
//Add one more Field    
$scope.labourdata = [{}];
$scope.addFormField = function(mobile) 
{
    var mobile = "";
    var found = $scope.labourdata.reduce(function(previous, i)
    {
        if (mobile === i) return true;
        return previous;
    }, false);
    if (found){
        alert('already taken.');
    }
    else
    {
        alert('ok');
        $scope.labourdata.push({});
    }
}

无法验证ng-repeat中的输入字段。

如果我们将Mobile No输入归档为'9999999999'。它在NAME输入字段中显示“XXX”名称。

当我点击“添加”按钮时,会出现下一行。问题是在添加一次Mobile No input作为'9999999999'时。  它应该是警觉的,因为它已经采取。因为我之前已经选择了移动号码'9999999999'。

如何在单击“添加”按钮时验证迭代中的输入数据。 有没有办法解决这个问题?

0 个答案:

没有答案