AngularUI Router is a routing framework for AngularJS, mainly developed to improve as well as enhance routing capabilities. It allows us to well organize the parts of application interface into a state machine. UI-Router is state-based framework making routing and UI navigation easier and manageable. The $route service in Angular core is organized around URL routes whereas UI-Router is organized around states. It may optionally have routes and other behaviour, attached with it.
States are bound to named, nested and parallel views that allow us to powerfully manage our application's interfaceInitial steps to use AngularUI Router :
Configuring our application with AngularUI Router :
Let’s begin configuring our application with AngularUI Router.
angular.module ( ‘ myUiRoutingApp ’ , [ 'ui.router'] )
2. After that we need to inject $stateProvider and $urlRouterProvider into our config object.
angular.module(‘myUiRoutingApp’, ['ui.router']).config ([ '$stateProvider', '$urlRouterProvider',
function( $stateProvider, $urlRouterProvider ) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state ('home', {
url: "/home",
templateUrl: "modules/main/views/home.html"
})
.state ('user', {
url: "/user",
templateUrl: "modules/user/views/user.html"
})
.state ('user.newUser', {
url: "/newUser'",
templateUrl: "modules/user/views/newUser.html"
}) }
]);
In example above $urlRouterProvider.otherwise(), is used to map our application to home state by default whereas $stateProvider is used to define states.
A state is nothing but a place in the application in terms of the overall UI and navigation perspective. It describes view (html template) it renders as well as how and what UI looks like at that place.
// $stateProvider ‘s state() method take unique stateName (String) and a stateConfig (Object)
$stateProvider.state( stateName, stateConfig);
Let’s have a look at some parameters often used to define state object.
These parameters for $stateProvider‘s .state () method are:
This is unique name describing state, e.g. "home”, “settings” etc. Usually stateName corresponds to view it renders. For having an inheritance based nesting of states that is for creating parent/child states, we use a dot notation. Example: "settings.profile", "user.newUser". Such states are defined when using nested states and nested views.
The stateConfig object can be defined using following properties.
This views object has following format.
3. Now we can add a ui-view directive to the <body /> of our app.
the ui-view is directive provided by the ui.router module. The ui-view directive gives information to $state like where to place our (views) templates. A view can be unnamed or named.
We can only have one unnamed view within any template. For having multiple views, we can do setup views property of $stateProvider’s state and use named views with ui-view directive. This can be better illustrated with following example.
<div ui-view=”headerPanel”></div>
<div ui-view="mainView"></div>
<div ui-view="footerPanel"></div>
$stateProvider.state ("home", {
views: {
"headerPanel": {
template: "<h1> Header data </h1>"
},
"mainView": {
template: “<div> main view data </div>"
},
"footerPanel": {
template: "<div> Footer data </div>"
}
}
4. Further we can use ui-sref directive while navigation to particular state.This directive binds a link (<a> anchor tag) to a state. If the state has an associated URL with it, then ui-sref directive automatically create and update the href attribute by using $state.href() method. Thus, click event to such a link will trigger a state transition with optional parameters.
Example:
HTML Template using ui-sref directive :
<a ui-sref="home">Home</a>
<a ui-sref="user.newUser">New User</a>
AngularUI Router usage benefits:
Ui-Router supports not only ngRoute but also adds many extra routing features.
Thus, In AngularJS applications for effective routing and navigation ui-router usage will be helpful.