我在角度方面遇到了非常奇怪的问题。我在 run 方法中定义了 $rootScope.user_info 。但是当我在控制器中获取根范围时,有时我会在控制器中得到未定义的方法 $rootScope.user_info。知道为什么在我刷新页面时有时会发生这种情况吗?
这是代码片段,
myApp.run(['$http', '$rootScope','$location','localStorageService','$window','$translate','$timeout', function($http, $rootScope,$location,localStorageService,$window,$translate,$timeout) {
$rootScope.current_user_id = localStorageService.get("CurrentUserId");
$rootScope.get_user_profile = function() {
$http.get('/api/v1/employees/'+$rootScope.current_user_id).success(function(response) {
$rootScope.user_info = response["data"]["user_info"];
});
};
if ($rootScope.current_user_id) {
$rootScope.get_user_profile();
}
}]);
myApp.controller('ProfileCtr', ['$rootScope','$scope','Employee','fileReader','$translate','$filter','checkResponse','toaster', function($rootScope,$scope, Employee,fileReader,$translate,$filter,checkResponse,toaster){
$scope.langs = [ { name: "en",value: "English"}, { name: "de_DE", value: "German"}, { name: "fr_FR", value: "French"} ];
$scope.set_language = function() {
$scope.selectLang = $filter('filter')($scope.langs, { name: $rootScope.user_info.eselected_language})[0];
}
$scope.set_language();
});