Send $ http request before initializing routing?

I have an event $rootScope.$on("$stateChangeStart")that changes the title and description .
 And I have to use the object $rootScopecreated after the request $httpin mine event. The problem is that when the event $stateChangeStartfires, mine objectis undefined.

I tried to use resolvein my ui-routerstates. But it only works for the controller, not event.

Could you help me come up with a solution that will start my service (with the request $http) before the routing $stateChangeStartstarts (and fire events), And it will not start this service again after state changes.

+4
source share
1 answer

I decided to try to solve this with promise:

$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){

    if ($rootScope.myObj) { // if my "object" was already created
        eventLogic();
    } else {
        getMyObj.init().then(function(res){ //call service that retuns a promise
            eventLogic();
        });
    }

    function eventLogic() {
        ...
    }
}

This solution completely solves my problem. It expects creation $rootScope.myObjand starts this service only once.

+2
source

All Articles