如何避免在下一个输入字段中显示所选选项?

时间:2016-08-03 11:24:14

标签: javascript angularjs

我有两个输入字段(输入字段1和输入字段2),它们具有自动填充选项。如果我选​​择输入字段1中的一个选项,则选择的选项不应显示在输入字段2的选项中。我可以这样做吗?

input field 1
<input type="text" ng-model="type1"  uib-typeahead="value for value in gettype($viewValue)" >

 input field 2
 <input type="text" ng-model="type2"  uib-typeahead="value for value in gettype($viewValue)">

$scope.gettype = function(val) {
return $http.get('api', {
  params: {
    type: val,
    sensor: false
  }
}).then(function(response){
  return response.data.type;
});
};

这只是一个示例代码,我计划做的是创建一个ng-repeat,它按照我的意愿重复输入字段

2 个答案:

答案 0 :(得分:1)

你可以使用ng-blur选项

将autofille_data视为您的数据

我们可以在ng-blur上调用一个函数

$scope.remode(autofill_data){
var index=autofill_data.indexOf($scope.input_val1);
 autofill_data.splice(index, 1);
}

其中$ scope.input_val1是输入字段1中的值

答案 1 :(得分:0)

重复创建文本框。     在数组之后,您需要删除已选择的值。例如:

html code

<span"><input type="text" ng-model="x.name1"</span>
<span"><input type="text" ng-model="x.name2"</span>
<span"><input type="text" ng-model="x.name3"</span>

js code:
Suppose your suggestion values are coming from array named $scope.usStates.
now you have to remove compare both value inside x an $scope.usStates and remove the matching element from $scope.usStates. you can use index of particullar element and splice method for removing an element.
example:
$scope.usStates.splice(index, 1);

$scope.usStates = [
    { name: 'ALABAMA', abbreviation: 'AL'},
    { name: 'ALASKA', abbreviation: 'AK'},
    { name: 'AMERICAN SAMOA', abbreviation: 'AS'},
    { name: 'ARIZONA', abbreviation: 'AZ'},
    { name: 'ARKANSAS', abbreviation: 'AR'},
    { name: 'CALIFORNIA', abbreviation: 'CA'},
    { name: 'COLORADO', abbreviation: 'CO'},
    { name: 'CONNECTICUT', abbreviation: 'CT'},
    { name: 'DELAWARE', abbreviation: 'DE'},
    { name: 'DISTRICT OF COLUMBIA', abbreviation: 'DC'},
    { name: 'FEDERATED STATES OF MICRONESIA', abbreviation: 'FM'},
    { name: 'FLORIDA', abbreviation: 'FL'},
    { name: 'GEORGIA', abbreviation: 'GA'}]

Thanks.