我有json数组,我想检查输入字段的值是否存在于数组中。
<input type='text'id='seat_number' name='seat_number' ng-model="employeeData.new_seat_number">
我的数组是employeeData.seat_array
。
如何使用Angular?
$scope.$watch('employeeData.new_seat_number', function (newVal) {
console.log(newVal);
$.each(employeeData, function (i, obj) {
if (obj.seat_number === $scope.employeeData.new_seat_number) {
alert("asd");
}
});
});
数组结构:
[{"seat_number":"834"},{"seat_number":"8F3"},{"seat_number":"891"}]
答案 0 :(得分:2)
var inArray = $scope.employeeData.seat_array.indexOf($scope.employeeData.new_seat_number) > -1;
我不确定employeeData.seat_array
的结构,但是如果它的对象数组可以使用Array.prototype.some
而不是indexOf:
var inArray = $scope.employeeData.seat_array.some(function(obj) {
return obj.seat_number === $scope.employeeData.new_seat_number;
});
答案 1 :(得分:1)
我找到了简单的解决方案
$scope.checkc = function(){
if(($scope.employeeData.seat_array.indexOf($scope.employeeData.new_seat_number))>-1)
{
alert('New seat number not available');
$scope.employeeData.new_seat_number="";
}
}