$routeProvider api

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 11 years ago
Updated 5 years ago

AngularJS's $routeProvider has a very basic api for defining your application's routes. $routeProvider.when() is used to match a url pattern to a view while $routeProvider.otherwise() is used to render a view when there is no match to a url pattern.

John Lindquist: The route provider has a very, very basic API. You can only either call "when" or "otherwise."

"When" is going to match a pattern and let's format this real quick. A lot of people do it this way although I put "when" on the next line and because it does method chaining, you can just do another "when" here.

Let's say, "when pizza." That will pass an object and we'll just say template because if you remember back to the template cache, we can just do template URL to pull it in by the key and all that stuff.

Anyway, so template. We'll just say the template is "Yum!!" because pizza is delicious. Then if I refresh my app and go to the URL of "/pizza," it will say "Yum!!" Don't worry, we will talk about what we can do with this hash later on.

We have "when" and then "otherwise." In "otherwise," you don't pass into route because it's whatever happens when the route doesn't exist so we just pass it as an object. We'll say the template here is, "This doesn't exist!"

If we put in like the number one, it says, "This doesn't exist!" Or some random string of characters, doesn't exist! If we put in pizza, it's going to pull in this template. If we put in the base URL, it's going to bring in our app template.

That's pretty much it as far as the route provider API goes, just "when" and "otherwise." We'll go into these options that you can pass into this object in the next video.

egghead
egghead
~ an hour 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