我需要在控制器之間使用i18n轉換功能。如何刪除控制器之間的重複共享功能
所以每個控制器應該有getCityName
和getCountryName
將其翻譯成正確的語言。
我試圖使用Angular服務來減少重複。如你所見,我仍然需要分別在兩個控制器中定義這些功能。
在我的情況下,是否可以刪除控制器中的定義函數的重複?
由於
Departure.html<div ng-repeat="departure in departures_lst">
<div class="panel-title">
<h5>{{getCountryName(departure.country)}}</h5>
</div>
<li ng-repeat="city in departure.cities">
<a ng-click="get_destinations(city)">
{{getCityName(city)}}
</a>
</li>
</div>
Arrival.html
<div ng-repeat="arrive in arrives_lst">
<div class="panel-title">
<h5>{{getCountryName(arrive.country)}}</h5>
</div>
<li ng-repeat="city in arrive.cities">
<a ng-click="get_destinations(city)">
{{getCityName(city)}}
</a>
</li>
</div>
角JS
爲了避免重複App.service('I18nService', function() {
this.getCountryName = function (country_name) {
return I18n.t("country." + country_name) + country_name
}
this.getCityName = function (city_name) {
return I18n.t("city." + city_name) + city_name
}
});
App.controller("departures_ctrl", function($scope, I18nService, $location, $http) {
$scope.getCountryName = function(country_name){
return I18nService.getCountryName(country_name);
}
$scope.getCityName = function(city_name){
return I18nService.getCityName(city_name);
}
});
App.controller("arrivals_ctrl", function($scope, $route, I18nService, $routeParams, $http, $window, $location) {
$scope.getCountryName = function(country_name){
return I18nService.getCountryName(country_name);
}
$scope.getCityName = function(city_name){
return I18nService.getCityName(city_name);
}
});
謝謝〜我會稍後再嘗試,但它看起來有點複雜,並把太多的邏輯放在這麼簡單的事情:) – newBike
@newBike很高興我幫了你。是的,只需要替換兩個函數就可能有點過於複雜,但它使您可以將您的函數邏輯全部放在一個地方而不是兩個地方,從長遠來看可以幫助您維護代碼或實施一些新的邏輯。 – fbid