This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

AngularJS Architecture: Basic State with ui-router

5:20 Angular 1.x lesson by

We will look at ui-router and learn how we can use it to define unique states with Eggly. This will allow us to leverage the power of a state machine in our AngularJS application, which goes beyond code management and into managing the states that exist within the code.

note the route doesn't actually fire unless you navigate to index.html#/ instead of index.html (see comments)

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

We will look at ui-router and learn how we can use it to define unique states with Eggly. This will allow us to leverage the power of a state machine in our AngularJS application, which goes beyond code management and into managing the states that exist within the code.

Avatar
Andy

Upon downloading the finished source code for this lesson, it doesn't seem to be working properly. I thought it was something in my code that might be the problem - maybe copied something from the video incorrectly. However, after downloading the source code and resolving all the External Library source issues, the page is not pulling in the template. There are also no errors in the JS Console so I'm at a loss.... Can you please double check the source code that is on github is in fact not working properly and let me know?

Avatar
Lukas

Andy -- the problem is that the route doesn't actually fire unless you navigate to index.html#/ instead of index.html. This gets fixed in a later lesson as I force the default route to always be '/'. Please let me know if you have any more questions. Happy to help!

In reply to Andy
Avatar
Xavier

Thanks for that! I started looking in my everywhere in my...

In reply to Lukas
Avatar
Ceejay

I was very confused why my code wasn't working until I read this. Thanks!

In reply to Lukas
Avatar
Taylor

I'm getting a CORS error when trying to load src/app/categories/categories.tmpl.html. As far as I can tell, I'm requiring and referencing things in the correct fashion. I'm running the app directly from localhost. The domain in my browser and in the error seem to be the same.

Is it because ui-router uses HTTP requests to fetch files? Do I need to actually deploy this application locally somehow?

Avatar
Lukas

Hi Taylor --

Are you serving the project from an HTTP server? I generally run it out of WebStorm which gives me one for free or you can use the npm module serve which is also very handy.

In reply to Taylor
Avatar
chad

I would of liked to have seen you follow your module/sub module pattern and write the routes for the modules into the modules themselves vs applying them to your "app.js" file.

Avatar
Lukas

Keep watching! :D I definitely end up there when it is all said and done.

In reply to chad
Avatar
Leo

Hi Lukas, please make sure you warn it on the video. Took me more than 3 hours to get it done.

I almost gave up, but than, I realise that I should've read all comments and here it is.

It worked with /#/.

Thanks anyway.

In reply to Lukas
Avatar
Lukas

Thanks Leo! Will add that to the description. #highFive

In reply to Leo
Avatar
Ezequiel

small contibution here...

go into 'src' folder or wherever the index.html file is located.

$python -m SimpleHTTPServer

then in your browser: localhost:8000 and that solves the CORS and avoid setting the '#/' manually at the end of the url

please let me know if this works for you
;)

In reply to Lukas
Avatar
Ezequiel

btw firing up a server "solves" the //xxx.js files calls in html... without a server you had to "fix" the paths forcing to --> html://cdnjs..../xxx.js otherwise the browser will try to locate those dependencies in --> "file://cdnjs.../xxx.js"

May I have Kudos? :P

In reply to Ezequiel
Avatar
Jonathan

Hey Lukas,

Can you explain why only 'index.html#/' works? Super confused here :S...

In reply to Lukas
Avatar
Lukas

Hi Jonathan --
The reason only index.html#/ works right now is because we haven't set up a fall back when a route is not matched precisely.

We will fix that in the next lesson when we add in $urlRouterProvider.otherwise('/'); via the $urlRouterProvider service.

In reply to Jonathan
Avatar
Jose L Rguez-Campra Camberos

I want to know if the code in https://github.com/eggheadio/egghead-angularjs-eggly-architecture/tree/03-ui-router-basic-state is ok?? I think that it does not work correctly.

Avatar
Lukas

Please check the previous comment.

In reply to Jose L Rguez-Campra Camberos
Avatar
Tri

Thanks for this tip!

In reply to Lukas
Avatar
John

Just a quick note, there is a typo at 3:18 in lesson 3 it shows eggly.models.model right at the end.

Avatar
Brian

Hi Lukus,
Either my IDE or my inexperience won’t allow me to use index#/. So, I found two other ways to solve this problem. One using $urlRouterProvider which you apply in ensuing lessons and the other way I found is by including the run method to navigate to the initial state. As follows:
.run(['$state', function ($state) {$state.transitionTo('eggly'); }])
I don’t know what pitfalls (if any) lay ahead by using the run method but for this lesson it works well.
Thanks, Brian

Avatar
Lukas

Hi Brian,
Did you try using index.html#/? I see that you used index#/ (which does not work for me either) but if you use index.html#/, the state should resolve. Hope that helps!

In reply to Brian
Avatar
Jonathan Case

Hey Lukas,

Is there any particular reason that you source the ui router from your own files in vendor, rather than pull from a cdn like you do for the rest?

Thanks!

Avatar
Lukas

Hi, Jonathan -- I was probably working on a plane that day :D In all seriousness, I prefer to use NPM to satisfy my dev dependencies these days.

In reply to Jonathan Case
Avatar
Borja

Dear Lukas,

perhaps this is a silly question but does ui-router form part of the official angularjs code or did you get it from somewhere else?
I know I can just copy the file from the github repo but I'd like to get it from the original source so I know where to find it for future reference.
Thanks!

Avatar
Lukas

Hi Borja - ui-router is a standalone project that you can find here https://github.com/angular-ui/ui-router

Hope that helps!

In reply to Borja
Avatar
Sina

Hi Lukas!
When I try to access " eggly/src/index.html#/ ", it gets changed to " eggly/src/index.html#!#%2F ". Therefor I can't see the ui-view. Any suggestions?

Hello, welcome back. This is Lucas Ruebbelke. In this lesson we are going to introduce ui-router into Eggly, so that we can start to navigate between the unique states that occur as a result of user interaction.

Let me give you an example. If the user selects a design category, that would result in a unique state in the application of only showing the design bookmarks. An extension of that idea would be if a user clicks on "Edit" for a bookmark, that state of editing that bookmark is unique. ui-router allows us to define and navigate between these states in a really flexible, easy yet powerful way.

The beauty of ui-router is that if you understand how to use ng-route it is really easy to leap from one to the other, as routes and states often share the same components.

For instance, a state could have an optional URL path, a template URL, and a controller. From there, the similarities tend to end as ui-router gives us a lot more functionality. The most notable one is the ability to have named or nested views, which we will get into in the next lesson.

For now, let's build out our first state, just to see this in action and how this actually looks.

The first thing we need to do is to jump into our HTML and add in the ui-router source code. I have this stored in the "vendor" directory.

The next thing we need to do from there is add in the ui-router sub-module so that it is available for the entire Eggly application. There we go.

Let's build out our first state. We're going to do this in the .config block, and we're going to inject $stateProvider, which is the mechanism for defining your states.

We'll define our first state as "eggly." The second parameter of this is essentially a configuration object, and we're going to give it a URL of "/" which is essentially the root of the Eggly application. Then we're going to give it a template URL of "app/categories."

For now, we're just going to store everything in the "categories" template. Then we will define the controller as "MainCtrl" since it is handy, and functional.

Let's hop into our HTML. What I'm going to do here is I am going to chop out most of the HTML that we have here. On this div with the "container-fluid" I am going to attach this "ui-view" attribute. Using ng-route it would be "ng-view", in this case it is "ui-view".

Let's hop into the "categories" template. For now I'm just going to do "HELLO UI ROUTER!". This is just to show that it is working.

I'm going to refresh the page, and we've got a big "HELLO UI ROUTER!" What it did is looked for that ui-view, and it inserted that template in there as well as spun it up with the main controller.

Obviously that's not very useful, so I'm going to paste our application in here, or paste the contents of the main HTML page in here. Let's refresh the page. From here it looks just like how we had it, but we've started to introduce the state machine.

In the next lesson we're actually going to introduce name views where we will actually have a specific view for categories, and a specific view for bookmarks.

Things start to get really powerful, and you can do some really neat things by targeting specific pieces of your layout with templates for specific states, as well as organizing your controllers into your view. Now you no longer have a large controller, but you have very small, granular controllers for specific pieces in your page.

Stay tuned for the next lesson, I'll see you then.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?