I need to use the i18n betweens translation features.
Therefore, each controller must have getCityNameand getCountryNamein order to translate it into the correct lang.
I tried using the Angular service to reduce duplication.
As you can see, I still need to define these functions in the two controllers separately.
Is it possible to remove duplication of defining functions in controllers in my case?
thanks
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>
Angular 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);
}
});
source
share