Search code examples
javascriptangularjsjsonangularjs-directiveangularjs-scope

how to select few json values in drop down in angularjs


i am getting a data from json file and the values using in dropdown but i dont want to use all values,I want to select only three values in my dropdown.


Solution

  • You can filter the list at the time of feeding it to the dropdown.

    <select ng-model="toType" class="form-control" required ng-change="ConvertCurrency()" 
    ng-options="k for (k, v) in (rates | 'yourfilter') track by v"></select>
    

    [UPDATE]

    I tried to access the API, and found out that res.data.rates is an object and not an array.

    So if you only have to show these 3 values, the easy approach would be to create a new array in controller:

    var ratesToDisplay = []; 
    ratesToDisplay.push({rate:'USD', value: res.data.rates.USD});
    ratesToDisplay.push({rate:'EUR', value: res.data.rates.EUR});
    ratesToDisplay.push({rate:'CAD', value: res.data.rates.CAD});
    

    and feed this new filtered array to your dropdown.

      <select ng-model="selectedRate" ng-options="rate.value as rate.rate for rate in ratesToDisplay">
        <option value="" disabled>Please Select</option>
      </select>
    

    I hope this helps.

    For 2nd part of your problem

    You can implement the ng-change() method and call the API from that method. Ex:

        function onDDChange(){
         $http.get('http://api.fixer.io/latest?' + $scope.fromType.label)
    .then(function(res) { \\logic for success call back 
        \\ call convert currency method from here.
        });
        } 
    

    HTML:

        <select ng-model="fromType" class="form-control" required 
    ng-change="onDDChange()" ng-options="f as f.label for f in rates">