Aswin cj Aswin cj - 4 months ago 10
Javascript Question

how to avoid showing selected option in next input field?

I have two input fields(input field 1 & input field 2) which have autofill options.if i select one of the options in input field 1 then that selected option should not show in the options of input field 2. how can i do that?

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;
});
};


this is just a sample code what im planning to do is to create a ng-repeat which repeats the input field as much as i wanted

Answer

repeat for creating text box. after that from array you need to remove those values which has been selected. for example:

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.
Comments