Resolve

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 11 years ago
Updated 5 years ago

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.

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.

egghead
egghead
~ 33 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today