我在角度上遇到了很奇怪的问题。我已经在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();
});
检查$ rootScope.user_info控制台run方法的内在价值。看看它是否在那里。 – Ved
@Ved如果我把console.log放在$ http回调里面,而不是打印这个对象,但是如果我把console.log放在get_user_profile回调的外面,比打印未定义的 – user9392255
等。我正在为此工作。 – Ved