You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.9 KiB

  1. # AngularUI Router  [![Build Status](https://travis-ci.org/angular-ui/ui-router.svg?branch=master)](https://travis-ci.org/angular-ui/ui-router)
  2. #### The de-facto solution to flexible routing with nested views
  3. ---
  4. **[Download 0.2.11](http://angular-ui.github.io/ui-router/release/angular-ui-router.js)** (or **[Minified](http://angular-ui.github.io/ui-router/release/angular-ui-router.min.js)**) **|**
  5. **[Guide](https://github.com/angular-ui/ui-router/wiki) |**
  6. **[API](http://angular-ui.github.io/ui-router/site) |**
  7. **[Sample](http://angular-ui.github.com/ui-router/sample/) ([Src](https://github.com/angular-ui/ui-router/tree/gh-pages/sample)) |**
  8. **[FAQ](https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions) |**
  9. **[Resources](#resources) |**
  10. **[Report an Issue](https://github.com/angular-ui/ui-router/blob/master/CONTRIBUTING.md#report-an-issue) |**
  11. **[Contribute](https://github.com/angular-ui/ui-router/blob/master/CONTRIBUTING.md#contribute) |**
  12. **[Help!](http://stackoverflow.com/questions/ask?tags=angularjs,angular-ui-router) |**
  13. **[Discuss](https://groups.google.com/forum/#!categories/angular-ui/router)**
  14. ---
  15. AngularUI Router is a routing framework for [AngularJS](http://angularjs.org), which allows you to organize the
  16. parts of your interface into a [*state machine*](https://en.wikipedia.org/wiki/Finite-state_machine). Unlike the
  17. [`$route` service](http://docs.angularjs.org/api/ngRoute.$route) in the Angular ngRoute module, which is organized around URL
  18. routes, UI-Router is organized around [*states*](https://github.com/angular-ui/ui-router/wiki),
  19. which may optionally have routes, as well as other behavior, attached.
  20. States are bound to *named*, *nested* and *parallel views*, allowing you to powerfully manage your application's interface.
  21. Check out the sample app: http://angular-ui.github.io/ui-router/sample/
  22. -
  23. **Note:** *UI-Router is under active development. As such, while this library is well-tested, the API may change. Consider using it in production applications only if you're comfortable following a changelog and updating your usage accordingly.*
  24. ## Get Started
  25. **(1)** Get UI-Router in one of the following ways:
  26. - clone & [build](CONTRIBUTING.md#developing) this repository
  27. - [download the release](http://angular-ui.github.io/ui-router/release/angular-ui-router.js) (or [minified](http://angular-ui.github.io/ui-router/release/angular-ui-router.min.js))
  28. - via **[Bower](http://bower.io/)**: by running `$ bower install angular-ui-router` from your console
  29. - or via **[npm](https://www.npmjs.org/)**: by running `$ npm install angular-ui-router` from your console
  30. - or via **[Component](https://github.com/component/component)**: by running `$ component install angular-ui/ui-router` from your console
  31. **(2)** Include `angular-ui-router.js` (or `angular-ui-router.min.js`) in your `index.html`, after including Angular itself (For Component users: ignore this step)
  32. **(3)** Add `'ui.router'` to your main module's list of dependencies (For Component users: replace `'ui.router'` with `require('angular-ui-router')`)
  33. When you're done, your setup should look similar to the following:
  34. >
  35. ```html
  36. <!doctype html>
  37. <html ng-app="myApp">
  38. <head>
  39. <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
  40. <script src="js/angular-ui-router.min.js"></script>
  41. <script>
  42. var myApp = angular.module('myApp', ['ui.router']);
  43. // For Component users, it should look like this:
  44. // var myApp = angular.module('myApp', [require('angular-ui-router')]);
  45. </script>
  46. ...
  47. </head>
  48. <body>
  49. ...
  50. </body>
  51. </html>
  52. ```
  53. ### [Nested States & Views](http://plnkr.co/edit/u18KQc?p=preview)
  54. The majority of UI-Router's power is in its ability to nest states & views.
  55. **(1)** First, follow the [setup](#get-started) instructions detailed above.
  56. **(2)** Then, add a [`ui-view` directive](https://github.com/angular-ui/ui-router/wiki/Quick-Reference#ui-view) to the `<body />` of your app.
  57. >
  58. ```html
  59. <!-- index.html -->
  60. <body>
  61. <div ui-view></div>
  62. <!-- We'll also add some navigation: -->
  63. <a ui-sref="state1">State 1</a>
  64. <a ui-sref="state2">State 2</a>
  65. </body>
  66. ```
  67. **(3)** You'll notice we also added some links with [`ui-sref` directives](https://github.com/angular-ui/ui-router/wiki/Quick-Reference#ui-sref). In addition to managing state transitions, this directive auto-generates the `href` attribute of the `<a />` element it's attached to, if the corresponding state has a URL. Next we'll add some templates. These will plug into the `ui-view` within `index.html`. Notice that they have their own `ui-view` as well! That is the key to nesting states and views.
  68. >
  69. ```html
  70. <!-- partials/state1.html -->
  71. <h1>State 1</h1>
  72. <hr/>
  73. <a ui-sref="state1.list">Show List</a>
  74. <div ui-view></div>
  75. ```
  76. ```html
  77. <!-- partials/state2.html -->
  78. <h1>State 2</h1>
  79. <hr/>
  80. <a ui-sref="state2.list">Show List</a>
  81. <div ui-view></div>
  82. ```
  83. **(4)** Next, we'll add some child templates. *These* will get plugged into the `ui-view` of their parent state templates.
  84. >
  85. ```html
  86. <!-- partials/state1.list.html -->
  87. <h3>List of State 1 Items</h3>
  88. <ul>
  89. <li ng-repeat="item in items">{{ item }}</li>
  90. </ul>
  91. ```
  92. >
  93. ```html
  94. <!-- partials/state2.list.html -->
  95. <h3>List of State 2 Things</h3>
  96. <ul>
  97. <li ng-repeat="thing in things">{{ thing }}</li>
  98. </ul>
  99. ```
  100. **(5)** Finally, we'll wire it all up with `$stateProvider`. Set up your states in the module config, as in the following:
  101. >
  102. ```javascript
  103. myApp.config(function($stateProvider, $urlRouterProvider) {
  104. //
  105. // For any unmatched url, redirect to /state1
  106. $urlRouterProvider.otherwise("/state1");
  107. //
  108. // Now set up the states
  109. $stateProvider
  110. .state('state1', {
  111. url: "/state1",
  112. templateUrl: "partials/state1.html"
  113. })
  114. .state('state1.list', {
  115. url: "/list",
  116. templateUrl: "partials/state1.list.html",
  117. controller: function($scope) {
  118. $scope.items = ["A", "List", "Of", "Items"];
  119. }
  120. })
  121. .state('state2', {
  122. url: "/state2",
  123. templateUrl: "partials/state2.html"
  124. })
  125. .state('state2.list', {
  126. url: "/list",
  127. templateUrl: "partials/state2.list.html",
  128. controller: function($scope) {
  129. $scope.things = ["A", "Set", "Of", "Things"];
  130. }
  131. });
  132. });
  133. ```
  134. **(6)** See this quick start example in action.
  135. >**[Go to Quick Start Plunker for Nested States & Views](http://plnkr.co/edit/u18KQc?p=preview)**
  136. **(7)** This only scratches the surface
  137. >**[Dive Deeper!](https://github.com/angular-ui/ui-router/wiki)**
  138. ### [Multiple & Named Views](http://plnkr.co/edit/SDOcGS?p=preview)
  139. Another great feature is the ability to have multiple `ui-view`s view per template.
  140. **Pro Tip:** *While multiple parallel views are a powerful feature, you'll often be able to manage your
  141. interfaces more effectively by nesting your views, and pairing those views with nested states.*
  142. **(1)** Follow the [setup](#get-started) instructions detailed above.
  143. **(2)** Add one or more `ui-view` to your app, give them names.
  144. >
  145. ```html
  146. <!-- index.html -->
  147. <body>
  148. <div ui-view="viewA"></div>
  149. <div ui-view="viewB"></div>
  150. <!-- Also a way to navigate -->
  151. <a ui-sref="route1">Route 1</a>
  152. <a ui-sref="route2">Route 2</a>
  153. </body>
  154. ```
  155. **(3)** Set up your states in the module config:
  156. >
  157. ```javascript
  158. myApp.config(function($stateProvider) {
  159. $stateProvider
  160. .state('index', {
  161. url: "",
  162. views: {
  163. "viewA": { template: "index.viewA" },
  164. "viewB": { template: "index.viewB" }
  165. }
  166. })
  167. .state('route1', {
  168. url: "/route1",
  169. views: {
  170. "viewA": { template: "route1.viewA" },
  171. "viewB": { template: "route1.viewB" }
  172. }
  173. })
  174. .state('route2', {
  175. url: "/route2",
  176. views: {
  177. "viewA": { template: "route2.viewA" },
  178. "viewB": { template: "route2.viewB" }
  179. }
  180. })
  181. });
  182. ```
  183. **(4)** See this quick start example in action.
  184. >**[Go to Quick Start Plunker for Multiple & Named Views](http://plnkr.co/edit/SDOcGS?p=preview)**
  185. ## Resources
  186. * [In-Depth Guide](https://github.com/angular-ui/ui-router/wiki)
  187. * [API Reference](http://angular-ui.github.io/ui-router/site)
  188. * [Sample App](http://angular-ui.github.com/ui-router/sample/) ([Source](https://github.com/angular-ui/ui-router/tree/gh-pages/sample))
  189. * [FAQ](https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions)
  190. * [Slides comparing ngRoute to ui-router](http://slid.es/timkindberg/ui-router#/)
  191. * [UI-Router Extras / Addons](http://christopherthielen.github.io/ui-router-extras/#/home) (@christopherthielen)
  192. ### Videos
  193. * [Introduction Video](https://egghead.io/lessons/angularjs-introduction-ui-router) (egghead.io)
  194. * [Tim Kindberg on Angular UI-Router](https://www.youtube.com/watch?v=lBqiZSemrqg)
  195. * [Activating States](https://egghead.io/lessons/angularjs-ui-router-activating-states) (egghead.io)
  196. * [Learn Angular.js using UI-Router](http://youtu.be/QETUuZ27N0w) (LearnCode.academy)
  197. ## Reporting issues and Contributing
  198. Please read our [Contributor guidelines](CONTRIBUTING.md) before reporting an issue or creating a pull request.