2

I am developing an angular js application, and i need to run a function, via service provider, before everything else. The function is a get request, which returns two variables in JSON, which returns either true orfalse. If the variable is true, then load another state (i am using ui router). How can i do that?

2 Answers 2

3

In angular the document loaded is not really has a good usage, because it is a framework and the we need to count on angular loaded/ready instead. Use angular.module('yourApp').run() like below:

var app = angular.module('yourApp');

app.run(['$rootScope', '$http', function($rootScope, $http){

    $http('http://localhost/status/').then(function(res){
        $rootScope.somethingStatus = res; // assuming that your response is either true of false.
    });

    $rootScope.$watch('somethingStatus', function(nv){
        // here is where you know your request has been done...
        if(nv === true) {
            // ...
        }
        else if(nv === false) {
            // ...
        }

    });
}]);

NOTE: please aware what run in app.run() will be fired before any controller initialing or view rendering. So later on when you want to access the value of what you been got. Inject $rootScope to the controller (or any place) when you needed it.

EDIT: fix some typos. Updated answer again, credit to @georgeawg and @Pierre Emmanuel Lallemant. Thanks you guys for the correction

Sign up to request clarification or add additional context in comments.

10 Comments

The documentation for the run method -- docs.angularjs.org/api/ng/type/angular.Module#run
Please fix you code. There are so many errors in it that it's not funny.
First problem -- Your closing square bracket is in the wrong place. Next problem -- $http returns a promise not a status. The result inside the .then should be put on the $rootScope. The httpPromise can go in a var.
you should set $rootScope.somethingStatus = res; in the callback. like @georgeawg said. upvoted
What i mean with $watch is that you 'want the callback in which you change the location be called once the $rootScope.somethingStatus is changed'. You can't just do a test with ===
|
2

You should write a controller for the page where you do the request (in order to display something to the user), with a default html view, and when you receive the response, then you change the state to use the desired controller.

4 Comments

I like your approach. If the request fails the user can get an error message. Also doing things in a .run block with $rootScope is hacky and has testability issues.
@georgeawg: It depends when we need to have the call. If you need the call to be done when you are in specific routes, you will put the $http call in the desired controllers. If you need the call to be executed in any page (for example retrieve the user information from the API, when you use Ctrl+R in any page) you should put it in the .run .
Can't retrieving user information be put in the routes resolver function? Resolver functions accept promises and route loading waits for those promises to resolve.
@georgeawg: never used it, I should look at. thx for the tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.