angularjsionic-v1angularjs-select

How to change variable properties according to drop-down menu item?


I have a drop-down menu populated with ng-options.

<label class="item item-input item-select">
    </div>
    <div class="input-currencies">
      Select Currency
<select ng-options="c.code as c.name for c in currencies" ng-model="codeMod" ng-change="currencyChange(codeMod)">
    </select>
  </label>

How do I get the value c.code to be the property of the variable named selectedCurrency?

.controller('CurrencyController', function($scope, $http) {  

 var ngb_currencies = 'http://lari.jumpstart.ge/en/api/v1/nbg_currencies?callback=JSON_CALLBACK'


var selectedCurreny = "" // I want get dropdown code in this variable depending on the selected currency so I make changes in the API


         $http.jsonp(ngb_currencies).success(function(currency) {
                $scope.currencies =   currency.results;
          })
            .error(function(data) {
              alert("ERROR");
            });


   $http({
    method: 'jsonp',
    url: 'http://lari.jumpstart.ge/en/api/v1/nbg_rates?callback=JSON_CALLBACK',
    params: { currency: selectedCurreny }
}).success(function(data, status , header, config) {
    console.log('success');
       $scope.result = data.result;
        console.log('success');
}).error(function(data, status , header, config) {
    console.log('error');
});

Solution

  • First, you have to change c.code as c.name for c in currencies in the ng-options attribute, you can't use a property here, it becomes c as c.name for c in currencies

    Then, since you specified ng-model="codeMod", you can watch that variable in your controller, which becomes the following :

    .controller('CurrencyController', function($scope, $http) {  
    
        var ngb_currencies = 'http://lari.jumpstart.ge/en/api/v1/nbg_currencies?callback=JSON_CALLBACK'
    
    
       $http.jsonp(ngb_currencies).success(function(currency) {
              $scope.currencies =   currency.results;
        })
          .error(function(data) {
            alert("ERROR");
          });
    
        $scope.$watch('codeMod', function(newSelectedCurreny) {
    
          $http({
              method: 'jsonp',
              url: 'http://lari.jumpstart.ge/en/api/v1/nbg_rates?callback=JSON_CALLBACK',
              params: { currency: newSelectedCurreny}
          }).success(function(data, status , header, config) {
              console.log('success');
                 $scope.result = data.result;
                  console.log('success');
          }).error(function(data, status , header, config) {
              console.log('error');
          });
        }
    }