I have the following code:
angular.module('myApp')
.controller('MyCtrl', function ($scope,$rootScope, $filter, $location,$translate,$cookies,NavigationService) {
$scope.appVersion = "";
$scope.credentials = {};
$scope.userLanguage = null;
var init = function (){
authenticate();
getAppVersion();
};
var getAppVersion = function () {
NavigationService.getAppInfo()
.then(function (response) {
$scope.appVersion = response.data.build.version
});
};
var authenticate = function (credentials, callback) {
$rootScope.authorities = [];
$rootScope.currentUser = null;
NavigationService.doAuthentication(credentials).success(function (data) {
$rootScope.authenticated = !!data.name;
$rootScope.authorities = data.authorities;
$rootScope.currentUser = data.name;
$scope.userLanguage = data.userLang;
callback && callback();
}).error(function (response) {
$rootScope.authenticated = false;
callback && callback();
});
};
...
<div ng-show="authenticated" class="btn-group btn-group-xs btn_change_language">
<button ng-class="{'button_selected': userLanguage == 'de'}" class="btn btn-default"
ng-click="changeLanguage('de')" translate>
BTN_GERMAN_LANGUAGE
</button>
<button ng-class="{'button_selected': userLanguage == 'en'}"
class="btn btn-default" ng-click="changeLanguage('en')" translate>
BTN_ENGLISH_LANGUAGE
</button>
</div>
The problem is that when I open the browser and load the page, the ngClass directive get the initial value of userLanguage (null) and not the value that comes from my request (data.userLang), so the class "buttom_selected" is not applied. I`ve already debugged it and the value is comming ok, however the ngClass just update if I reload the page. The strange thing is that, if I open a new tab it works normally, but if I close and open the browser and access the application the ngClass ignores the change done by NavigationService callback.
I`ve tried $scope.$apply() but it says that $digest is already in progress.