我在角度上遇到了很奇怪的問題。我已經在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