I
I
Ivan2015-10-22 13:47:25
Angular
Ivan, 2015-10-22 13:47:25

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
        })
    });

How to save information in $scope in run?
Or perhaps you know how to make a request on every page and have the response available in all controllers?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2015-10-23
@ATNC

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.

N
Nicholas, 2015-10-22
@healqq

Use Factory to store data.

V
Vladislav, 2015-10-22
@Div100

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); //здесь уже в любом месте приложения получаете пришедшие  данные  
    })
}

or
AppRun.$inject = ['UserService'];
function AppRun(UserService) {
    UserService.loadUserData(); //В другом месте просто получаете из этого же сервиса данные, само собой там всякие проверки сделайте
})

Or you can cross the whole thing and use it together

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question