I have an AngularJS web application. I would like to change page´s title dinamically. I would like the language configured is taken into account, so title should be displayed in different languages.
I successed to change the title dinamically when I navigate to different pages. I got the third approach of this post How to dynamically change header based on AngularJS partial view? which looked the most simple for me (I mean the one using $rootScope).
I have just an issue. When I´m on a page, let´s say index, and let´s suppose the language configuration is english, the title is displayed correctly in english. But If I change the language, in example to spanish (in a dropdown in navigation bar), the title does not change. If I navigate to another page, the title is correctly displayed in spanish.
Please find, the relevant code:
HTML:
<title ng-bind="title"></title>
EACH CONTROLLER:
.controller('HomeCtrl', function HomeCtrl($scope, $rootScope, $translate) {
$rootScope.title = $translate('PAGE_TITLE_INDEX');
...
}
.controller ('AboutCtrl', function ($scope, $rootScope, $translate) {
$rootScope.title = $translate('PAGE_TITLE_ABOUT');
...
}
LANGUAGE SELECT DROPDOWN
<div ng-controller="LocationCtrl" style="padding-top: 5px">
<select class="bootstrap-select-language show-tick"
ng-change="changeLanguage(langKey)"
ng-model="langKey"
data-header="Choose your language..."
ng-options="language.locale as language.name for language in translationLanguages"
bs-select
data-width="150px">
</select>
</div>
TRANSLATION FUNCTION IN CONTROLLER
$scope.changeLanguage = function (langKey) {
$scope.langKey = langKey;
$translate.uses(langKey);
...
}
UPDATE
I think for this scenario, the first approach in the above post is the right one (I mean, keeping the title in a service and get and set from the controllers). This way you can get the current title value in the translations controller and change it dinamically. Right?