0

我有一个填充了 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');
});
4

1 回答 1

0

首先,你必须c.code as c.name for c in currenciesng-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');
      });
    }
}
于 2016-07-19T10:32:07.913 回答