Reputation: 11
I'm new to angularJS and working over an application where i switch over multiple languages. After i change the language and refresh the page it will switch to the default language (i.e)English. Please suggest how do i keep the value of $rootScope so that it will not change after refreshing the page
My CTRL---------
myApp.controller('recordCtrl',function($scope, $translate, $http,altaMdlSvcs,$rootScope,$cookieStore,altaPageActionSvcs,$location,$rootScope,$stateParams) { $scope.globalLang = '';
$http.get("metadata/record.json").then(function(response) {
$scope.recordArray = response.data.records;
});
$http.get("metadata/language.json").then(function(response) {
$rootScope.languages = response.data;
});
var locale = localStorage.getItem("token");
//$translate.use(locale);
$scope.changeLanguage = function (langObj) {
$scope.globalLang = langObj.lang.langCode;
localStorage.setItem("token", $scope.globalLang);
console.log("lower"+$scope.globalLang+"................");
$translate.use(langObj.lang.langCode);
};
$scope.openModel = function(langObj) {
var requestMap = {
"templateHtml" : '/I18nDemo/html/model.html',
"controllerName" : 'modelCtrl',
"doAction" : $scope.changeLanguage,
"lang" : langObj,
"parent" : $scope
};
altaMdlSvcs.customOpen(requestMap);
};
});
Upvotes: 1
Views: 3543
Reputation: 557
By refreshing the page (f5) you will wipe your $rootscope from memory. Your application restarts.
As mentioned above you should probably use some kind of storage. That way you can save a users preference and use it again when he comes back to you application.
Have a look at: https://www.npmjs.com/package/ng-storage
Upvotes: 1