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

    The config function

    John LindquistJohn Lindquist

    This episode explains the config phase of an AngularJS application and how is it special in regards to $routeProvider (routing configuration) and providers in general.



    Become a Member to view code

    You must be a 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




    John Lindquist: I want to take a second to talk about the "config function" and "providers" without going into too much detail because we will create our own providers later.

    We are going to be using the "route providers" quite extensively so let's dive into it. If I try and inject the route provider into my app controller and see if I reload this, it will say, "unknown provider, provider, provider." Meaning that it is looking for a provider with the word "provider" on the end. This is actually unavailable, because it is looking for a service being created by a provider.

    What that means is, what you actually do is you inject the route in here and it's going to find the route so no error. It's going to find the route because it found a provider.

    Now if I were to try to inject route in here, I see an unknown provider route.

    So at this point, in this phase, if your application is configuring it, all you have are these providers, which are the factories which generate an instance of things like "route," is the service that comes from the route provider.

    At this point, when you are configuring your app, you are basically saying, "When I generate route, this is how I want it to appear." If you were to try for instance, you can actually access all the stuff you see in here from the route itself. So you could say, "a route routes." Access it like this, then try and paste in this guy. That's totally the gist.

    It's just that if I do that, I am going to see a "Run Fine." So no errors were thrown. But what's going on is, this is way too late in the process because we need to reference the app controller so it can be initiated when you navigate to this URL. It has no idea that when you navigate to that URL that it wants to inaudible 02:03 app controller.

    Now you are going to see that its way too late. We can't find any controller and the services are not expected to do anything because the configuration needs to happen before you inject it into anything you want to use it in.

    Just a quick review, again, these providers are available in the config function. You can generate your own, we'll talk about that later. But in the config function, we're going to be injecting a provider which will then...When you try to access the instance of it like a route, inside the controller then you can get things. A pre-configured instance based on what you do to configure it in here.

    That's the basics of "provider" and we are going to dive a lot more into route provider here next.