我正在使用以下代码在 rootscope 中存储用户名('pqr')值,但它的范围是 $http.get mehtod
angular.module('myApp').run(function ($rootScope,$http) {
$rootScope.username = 'abc';
$http.get('/api/getuser')
.success(function(data) {
console.log("logged in user is: "+data);
$rootScope.username = data;
});
$rootScope.$on('$routeChangeStart', function (event, next) {
});
console.log("logged in user after setting: " + $rootScope.username);
});
控制台日志输出为:
logged in user is: pqr
logged in user after setting: abc
如何将 $rootScope.username 设置为 pqr 超出 $http.get 方法的范围
编辑:添加了右括号。