Introduction
This is the second article in series of articles for learning UI-Router. In this article, we will have an in-depth learning for state in ui-router.
In case you have not read previous article, you can find it Here,
Overview
What is ui-Router state?
A state normally references a “place” in an application where all the information of UI and Navigation exists. A state describes the UI of an application via the Controller/ template / view properties.
A state is a JavaScript object which has specific Properties. Those properties help in defining the functionality of application when a state is active, below are the properties of state.
- name: A name for the state, providing a way to refer to the state
- views: How the UI will look and behave
- url: What the browser’s URL will be
- prams: Parameter values that the state requires (such as blog-post-id)
- resolve: The actual data the state requires (often fetched, asynchronously, from the backend using a parameter value)
A state in its simplest form can be added like this (typically within module.config),
- <!-- in index.html -->
- <body ng-controller="MainCtrl">
- <section ui-view></section>
- </body> // in app-states.js (or whatever you want to name it) $stateProvider.state('contacts', { template: '
- <h1>My Contacts</h1>' })
Where is the Template viewed?
This is the question which will be raised in everyone’s mind. Whenever a state is activated the template is automatically rendered in the ui-view of its parent state’s template. If it's a top-level state—which 'contacts' is because it has no parent state–then its parent template is index.html.
Here in our example, the contacts state won’t ever be activated. So, let’s see how we can activate a state.
Activating a state
There are mainly three methods to activate a state as listed below.
- Call $state.go(). High-level convenience method.
- Click on the link written in ui-sref
- Navigate to URL associated with state.
All three methods are explained in detail below.
$state.go()$state
$state service is used for representing states and transition between them. It also provides information about the current state or even the states from which the transition is done.
$state.go(to, params, options)
| Param | Type | Details |
| to | string | Absolute state name or relative state path. Some examples:·
|
| params(optional) | object | A map of the parameters that will be sent to the state, will populate $stateParams. Any parameters that are not specified will be inherited from currently defined parameters. Only parameters specified in the state definition can be overridden, new parameters will be ignored. |
| options(optional) | object | Options object. The options are:·
|
ui-sref
It is a directive that binds a link(<a>) to a state. If there is an associated url in the state then the directive will automatically generate and update the href attribute using $state.href method.
Usage
- ui-sref='stateName' - Navigate to state, no params. 'stateName' can be any valid absolute or relative state, following the same syntax rules as $state.go()
- ui-sref='stateName({param: value, param: value})' - Navigate to state, with params.
Example
Template HTML
- <a ui-sref="home">Home</a> | <a ui-sref="about">About</a>
- <ul>
- <li ng-repeat="contact in contacts">
- <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
- </li>
- </ul>
URL
It is similar to the url that we have used in ngRoute
Here's how you set a basic url.
- $stateProvider .state('contacts', { url: "/contacts", templateUrl: 'contacts.html' })
Now when the user accesses index.html/contacts then the 'contacts' state would become active and the main ui-view will be populated with the 'contacts.html' partial.
TemplatesThere are several methods to configure states template.
As seen in above example the simplest way is to set template by template config property
- $stateProvider.state('contacts', { template: '<h1>My Contacts</h1>'})
Instead of writing template inline you can load a partial html page and probably it is the method that is used normally to insert template in a view.
- $stateProvider.state('contacts', { templateUrl: 'contacts.html'})
templateUrl can also be a function with a predefined parameter $stateParams which is not injected. This function returns URL.
- $stateProvider.state('contacts', {
- templateUrl: function ($stateParams){
- return '/partials/contacts.' + $stateParams.filterBy + '.html';
- }
- })
you can also customize it by using templateProvider which can be injected, has access to locals and must return template HTML as below,
- $stateProvider.state('contacts', {
- templateProvider: function ($timeout, $stateParams) {
- return $timeout(function () {
- return '<h1>' + $stateParams.contactId + '</h1>'
- }, 100);
- }
- })
Controllers
You can assign a controller to your template.
Note
The controller will not be instantiated if template is not defined.
You set your controller like this,
- $stateProvider.state('contacts', {
- template: '<h1>{{title}}</h1>',
- controller: function($scope){
- $scope.title = 'My Contacts';
- }
- })

Join the conversation! Your thoughts help the community grow.