2015-11-26 46 views
2

我有2個鏈接的JSON對象數組。
實施例:
國家:在AngularJs中鏈接兩個JSON對象

[{ 
     "countryCode":"IN", 
     "countryName":"India", 
     "currencyCode":"INR" 
    }, 
{ 
     "countryCode":"US", 
     "countryName":"United States", 
     "currencyCode":"USD" 
    }] 

貨幣:

[{ 
     "code":"INR", 
     "name":"Indian Rupee", 
     "locale":"kok_IN", 
     "display":1 
    }, { 
     "code":"USD", 
     "name":"US Dollar", 
     "locale":"en_US_POSIX", 
     "display":1 
    }] 

上述兩個JSON obejcts與一個代碼鏈接。 我試圖通過鏈接貨幣代碼來顯示貨幣對象的貨幣名稱。
我喜歡顯示波紋管:

<tr ng-repeat="country in countries | filter : query | orderBy : 'name'"> 
<td>{{ country.countryName }}</td> 
<td>{{ }}</td> <!-- Currency name here --> 
</tr> 

我怎麼在這裏顯示的貨幣名稱?

回答

1

對我的作品與功能getCurrencyByCountry:

angular.module("App", []).controller("AppController", function($scope) { 
 
    $scope.countries = [{ 
 
     "countryCode": "IN", 
 
     "countryName": "India", 
 
     "currencyCode": "INR" 
 
    }, { 
 
     "countryCode": "US", 
 
     "countryName": "United States", 
 
     "currencyCode": "USD" 
 
    }]; 
 
    
 
    $scope.currency = [{ 
 
     "code": "INR", 
 
     "name": "Indian Rupee", 
 
     "locale": "kok_IN", 
 
     "display": 1 
 
    }, { 
 
     "code": "USD", 
 
     "name": "US Dollar", 
 
     "locale": "en_US_POSIX", 
 
     "display": 1 
 
    }]; 
 
    
 
    $scope.getCurrencyByCountry = function(country){ 
 
     var currency = ""; 
 
     angular.forEach($scope.currency, function(value, key) { 
 
      if(country.currencyCode == value.code){ 
 
       currency = value.name; 
 
       return false; 
 
      } 
 
     }); 
 
     return currency; 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<table ng-app="App" ng-controller="AppController"> 
 
    <tr ng-repeat="country in countries |orderBy : 'name'"> 
 
    <td>{{country.countryName}}</td> 
 
    <td>{{getCurrencyByCountry(country)}}</td> 
 
    <!-- Currency name here --> 
 
    </tr> 
 
<table>

+0

謝謝,它工作:) –

0

我會在一個控制器功能實現

$scope.currencies = [{ 
    "code": "INR", 
    "name": "Indian Rupee", 
    "locale": "kok_IN", 
    "display": 1 
}, { 
    "code": "USD", 
    "name": "US Dollar", 
    "locale": "en_US_POSIX", 
    "display": 1 
}]; 
$scope.getCurrency(code) { 
    return $scope.currencies.find(c => c.code === code).name; 
} 

(讓我知道如果你需要這在ES5代替)

然後你可以使用

{{getCurrency(country.code)}} 
+0

你能解釋一下它在做什麼嗎? –

+0

與你接受的答案一樣,但在一行中,而不是10 –

+0

其實我無法理解如何使用它。 –