Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    AngularJS 1.2.x Routing Changes: ngRoute



    This video shows how the AngularJS 'resolve' feature can be used during the config phase before your controller has been instantiated. This gives you the opportunity to ensure everything has been setup properly that your controller depends on, thus allowing your controller to execute safely based on its assumptions.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    John Lindquist: The resolve property is basically a list of promises, of things that need to happen before your controller will instantiate, and your template will load and everything will set up. So, it's a way of routing to say, "Hang on. I've got some stuff to do first."

    Let's set up a resolve called "app" instead of a resolve property. This is basically a map two or app controller, and we'll just say this is a function. You get really, really used to an angular, everything being a function, so that you can inject stuff in. The CUE library is the promised library that we talked about, and from there we'll just say CUE, defer. Then we can return the promise from defer that promise.

    You'll see right now if we reload, nothing will happen because the promise hasn't been fulfilled. If we say, "defer resolve," and now we refresh, you can see that it works just fine, because we've returned a promise and the promise has resolved. That means that everything can move on just as you would expect it to.

    Say for example, what you'll see in a lot of the examples that show this, there's a time out service you can use. It basically means it's a way of wrapping set time out. Let's say, we'll resolve after two seconds. Now, when I refresh 1, 1,002, 1000, and then it shows up again -- 1, 1002, 1000, and then it resolves.

    This is also useful for things like using the http service and a whole bunch of other things you may want to do before your controller loads, and the template fires up, and you're ready to start working with what's changed in that route. We'll go into more detail in the next video.