Answer the question
In order to leave comments, you need to log in
Why can't $scope be defined in the run module?
Good day, gentlemen. You need to make a request in the run module and save the response in $scope or $rootScope. When I try to include $scope in a function, I get "Error: $injector:unpr
Unknown Provider"
app.run(function ($http, $scope) {
$http.get('/ru/user_data/').success(function (data) {
$scope.user_data = data.info
})
});
Answer the question
In order to leave comments, you need to log in
I'll probably answer the question asked.
Because run is not executed in the context of any controller and $scope cannot be there.
So I agree with the advice about using the factory.
Here are a couple of options:
1)
AppRun.$inject = ['$rootScope', '$http'];
function AppRun($rootScope, $http) {
$http.get('user_data/').success(function(data) {
$rootScope.$broadcast('userDataUpdated', data); //здесь уже в любом месте приложения получаете пришедшие данные
})
}
AppRun.$inject = ['UserService'];
function AppRun(UserService) {
UserService.loadUserData(); //В другом месте просто получаете из этого же сервиса данные, само собой там всякие проверки сделайте
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question