Software Development

AngularJS Routing Dynamically with UI-Router

[wp_ad_camp_5]

Previously, I wrote about routing using UI-router. This post demonstrates how to programmatically move from one state to another instead of clicking a set of  ui-sref links.

Contents

Software Requirements

UI-Router

Before UI-Router can be used, you have to download it together with the main AngularJS library. Then include them in the main HTML file along with your AngularJS codes.

[wp_ad_camp_1]

Instead of a set of ui-sref links, you’ll have buttons, when clicked, invoke JavaScript functions. These functions are defined in another controller named navByButtonController.

These functions are defined in another controller named navByButtonController.

To move between state, use this:

The name-of-state is any value set for the name property of JSON objects passed to $stateProvider.state(json_object);

Define Routes

The next thing to do is to define a number of routes using $stateProvider in the config function in myapp.js.

[wp_ad_camp_2]

We use $urlRouterProvider to set an initial or default URL.

UI-View Directive

The uiView directive as shown in the code snippet below is where the contents from the templateUrls get displayed per selected route, e.g., '/second'. The section is updated when the route changes.

Testing our SPA

[wp_ad_camp_3]

Download the codes

[wp_ad_camp_4]

https://github.com/Turreta/AngularJS-Routing-Dynamically-with-UI-Router

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like