如何根据下拉菜单项更改变量属性?

时间:2016-07-19 10:10:41

标签: angularjs ionic-v1 angularjs-select

我有一个填充了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>

如何将值c.code作为名为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');
});

1 个答案:

答案 0 :(得分:0)

首先,您必须更改c.code as c.name for c in currencies属性中的ng-options,您不能在此处使用属性,它会变为c as c.name for c in currencies

然后,由于您指定了ng-model="codeMod",您可以在控制器中查看该变量,该变量将变为:

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