Angularui Router Overview "Turn"

Source: Internet
Author: User

With Angularjs to create a spa (single page application), routing is important to make the page navigation look like a normal web page. Angularui router is a ANGULARJS routing module developed by the Angularui team, which is more flexible than Angularjs's standard routing ngroute, Load multiple views and maintain view levels on a page based on state rather than URL, Nested states & Views and multiple & Named views. Ui-router is considered to be one of the most practical modules Angularui provides for developers.

Compared with Ngroute:

    • $state $route
    • $stateParams $routeParams
    • $stateProvider $routeProvider
    • <div ng-view></div>, <div ui-view></div>


(1) Set up routing
Use $stateprovider (not $routeprovider) in the. config () mode:

JS Code
    1. $stateProvider. State (' state name ', {setup information});



However, the default otherwise needs to use $urlrouterprovider to set

JS Code
    1. $urlRouterProvider. Otherwise ('/tab/home ');



For example:

JS Code
  1. $stateProvider
  2. . State ("emp", {
  3. URL:"/emp"
  4. , Templateurl:"list.html"
  5. , controller:"Listctrl"
  6. })
  7. . State ("Emp.detail", {
  8. URL:"/:empid"
  9. , Templateurl:"emp.html"
  10. , controller:"Empctrl"
  11. });



How to set the Ngroute:

JS Code
    1. $routeProvider. When (' url ', {setup info});



For example:

JS Code
    1. $routeProvider
    2. . When ("/emp", {
    3. Templateurl:"list.html"
    4. , controller:"Listctrl"
    5. })
    6. . When ("/emp/:empid") {
    7. Templateurl:"emp.html"
    8. , controller:"Empctrl"
    9. };



(2) Hierarchical
The state can be nested, dividing the hierarchy by the points in the state name.
The format is: parent view. Child View
For example: Items.detail is a sub-view of items.

JS Code
  1. $stateProvider
  2. . State ("items", {
  3. Abstract: true,
  4. URL: "/items"
  5. Templateurl: "items.html" //must include <ui-view/>
  6. })
  7. . State ("Items.detail", {
  8. URL: " /detail", //URL is "/items/detail"
  9. Templateurl: "items-detail.html"
  10. })
  11. . State ("Items.info", {
  12. URL: " /info", //URL is "/items/info"
  13. Templateurl: "items-info.html"
  14. });



(3) Specify the view name
<div ui-view= "View1" ></div>
<div ui-view= "View2" ></div>

JS Code
  1. . State ("Emp.detail"), {
  2. URL:"/:empid"
  3. , views:{
  4. view1:{
  5. Templateurl:"view1.html"
  6. , controller:"View1ctrl"
  7. }
  8. , view2:{
  9. Templateurl:"view1.html"
  10. , controller:"View1ctrl"
  11. }
  12. }
  13. }



Initial view

JS Code
    1. . State ("Emp.detail"), {
    2. URL:"/:empid"
    3. , views:{
    4. "":{  
    5. Templateurl:"emp.html"
    6. , controller:"Empctrl"
    7. }
    8. }
    9. }



[Email protected]

JS Code
    1. . State ("Emp.detail.picture"), {
    2. URL:"/all"
    3. , views:{
    4. "@emp": {
    5. Templateurl:"picture.html"
    6. , controller:"Picturectrl"
    7. }
    8. }
    9. }



(4) Setting
URL: The default relative path (the absolute path begins with ^)
Views: Each child view can contain its own template, controller, and preload data.
Abstract: Abstraction template cannot be activated
Template, Templateurl, templateprovider:html string, or function that returns an HTML string, the path to an HTML template, or a function that returns an HTML template path, a function that returns an HTML string
Controller, Controllerprovider: Specify any controllers that are already registered or a function that acts as a controller
Resolve: Pre-load a series of dependencies or data before the route arrives and inject it into the controller.
Data: It is not injected into the controller, it is used to pass data from the parent state to the child State.
Onenter/onexit: These two functions are called when entering or leaving a view of the current state

Examples of URL use:
URL: '/inbox '
URL: '/inbox/:inboxid '
URL: '/inbox/{inboxid} '
URL: '/inbox/{inboxid:[0-9a-fa-f]{6}} '
URL: '/inbox/{inboxid:.*} '
URL: '/inbox?sort '
URL: '/inbox/:inboxid/messages/{sorted}?from&to '
URL: '/party/:p artyid/:p artylocation '
URL: '/product/info/favorite?pid&jancode&showaddfavorite&frompg '

(5) page Jump
<a href= "#/emp/hoge/1234" >Go</a>
<a ui-sref= ". Hoge ({id:empid})" >Go</a>
$state. Go (' state name ', {parameter});

(6) Event
State Event

    • $rootScope. $on (' $stateChangeStart ', function (event, tostate, Toparams, FromState, Fromparams) {...})
    • $rootScope. $on (' $stateNotFound ', function (event, unfoundstate, FromState, Fromparams) {...})
    • $rootScope. $on (' $stateChangeSuccess ', function (event, tostate, Toparams, FromState, Fromparams) {...})
    • $rootScope. $on (' $stateChangeError ', function (event, tostate, Toparams, FromState, Fromparams, error) {...})


View Event
View is loaded but before the DOM tree is built:
$scope. $on (' $viewContentLoading ', function (event, viewconfig) {...});
When view is loaded and the DOM tree is built:
$scope. $on (' $viewContentLoaded ', function (event) {...});

Reference:
Https://github.com/angular-ui/ui-router
Https://docs.angularjs.org/api/ngRoute
http://angular-ui.github.io/ui-router/sample/
Http://scotch.io/tutorials/javascript/angular-routing-using-ui-router
Http://blog.eisneim.com/articles/dive_deep_into_ui-router.html

Angularui Router Overview "Turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.