I want to set a $rootScope.userinfo through run, and then in different pages, I can also use $rootScope.userinfo to obtain the current user's information without going to the server to crawl it, but now I have encountered a lot of problems.
For the first time, I directly $http in run and then set $rootScope.userinfo=response, and then used $scope.userinfo=$rootScope.userinfo in each controller to capture this userinfo into the current scope for use, but An undefined problem occurred;
The second time I used broadcast in run
$rootScope.$broadcast("thisintheuserinfo", response);
Then use $on
in the controller$scope.$on("thisintheuserinfo",
function (event, msg) {
if(msg){
//$scope.usernamea = msg.user;
console.log('123')
console.log(msg)
}
else{
alert(msg)
}
});
But there is a problem that the code in the $on function almost never runs. It seems that because the one in run is asynchronous, the controller's on starts before the broadcast starts? No matter what, there are more wrong and less right.
So I don’t know if you have any good methods that I can learn from?
Basically meets the requirements, there may be a few side effects.