|
| 1 | +# AngularUI Router [](https://travis-ci.org/angular-ui/ui-router) |
| 2 | + |
| 3 | +**Note: this is the Angular 1.x source for UI-Router version 1.0 alpha. If you are looking for the source for UI-Router |
| 4 | +version 0.2.x, it can be found [here](https://github.com/angular-ui/ui-router/tree/legacy)** |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +#### The de-facto solution to flexible routing in angular |
| 10 | +--- |
| 11 | +**[Docs 1.0.0-alpha.3](http://angular-ui.github.io/ui-router/1.0.0-alpha.3)** | |
| 12 | +**[Download stable](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)**) **|** |
| 13 | +**[Guide](https://github.com/angular-ui/ui-router/wiki) |** |
| 14 | +**[API](http://angular-ui.github.io/ui-router/site) |** |
| 15 | +**[Sample](http://ui-router.github.io/sample-app/#/mymessages) ([Src](https://github.com/ui-router/sample-app)) |** |
| 16 | +**[FAQ](https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions) |** |
| 17 | +**[Resources](#resources) |** |
| 18 | +**[Report an Issue](https://github.com/angular-ui/ui-router/blob/master/CONTRIBUTING.md#report-an-issue) |** |
| 19 | +**[Contribute](https://github.com/angular-ui/ui-router/blob/master/CONTRIBUTING.md#contribute) |** |
| 20 | +**[Help!](http://stackoverflow.com/questions/ask?tags=angularjs,angular-ui-router) |** |
| 21 | + |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +Angular UI-Router is a client-side [Single Page Application](https://en.wikipedia.org/wiki/Single-page_application) |
| 26 | +routing framework for [AngularJS](http://angularjs.org). |
| 27 | + |
| 28 | +Routing frameworks for SPAs update the browser's URL as the user navigates through the app. Conversely, this allows |
| 29 | +changes to the browser's URL to drive navigation through the app, thus allowing the user to create a bookmark to a |
| 30 | +___location deep within the SPA. |
| 31 | + |
| 32 | +UI-Router applications are modeled as a hierarchical tree of states. UI-Router provides a |
| 33 | +[*state machine*](https://en.wikipedia.org/wiki/Finite-state_machine) to manage the transitions between those |
| 34 | +application states in a transaction-like manner. |
| 35 | + |
| 36 | +## Get Started |
| 37 | + |
| 38 | +**(1)** Get UI-Router in one of the following ways: |
| 39 | + - clone & [build](CONTRIBUTING.md#developing) this repository |
| 40 | + - [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)) |
| 41 | + - [link to cdn](http://cdnjs.com/libraries/angular-ui-router) |
| 42 | + - via **[jspm](http://jspm.io/)**: by running `$ jspm install angular-ui-router` from your console |
| 43 | + - or via **[npm](https://www.npmjs.org/)**: by running `$ npm install angular-ui-router` from your console |
| 44 | + - or via **[Bower](http://bower.io/)**: by running `$ bower install angular-ui-router` from your console |
| 45 | + - or via **[Component](https://github.com/component/component)**: by running `$ component install angular-ui/ui-router` from your console |
| 46 | + |
| 47 | +**(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) |
| 48 | + |
| 49 | +**(3)** Add `'ui.router'` to your main module's list of dependencies (For Component users: replace `'ui.router'` with `require('angular-ui-router')`) |
| 50 | + |
| 51 | +When you're done, your setup should look similar to the following: |
| 52 | + |
| 53 | +> |
| 54 | +```html |
| 55 | +<!doctype html> |
| 56 | +<html ng-app="myApp"> |
| 57 | +<head> |
| 58 | + <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script> |
| 59 | + <script src="js/angular-ui-router.min.js"></script> |
| 60 | + <script> |
| 61 | + var myApp = angular.module('myApp', ['ui.router']); |
| 62 | + // For Component users, it should look like this: |
| 63 | + // var myApp = angular.module('myApp', [require('angular-ui-router')]); |
| 64 | + </script> |
| 65 | + ... |
| 66 | +</head> |
| 67 | +<body> |
| 68 | + ... |
| 69 | +</body> |
| 70 | +</html> |
| 71 | +``` |
| 72 | + |
| 73 | +### [Nested States & Views](http://plnkr.co/edit/u18KQc?p=preview) |
| 74 | + |
| 75 | +The majority of UI-Router's power is in its ability to nest states & views. |
| 76 | + |
| 77 | +**(1)** First, follow the [setup](#get-started) instructions detailed above. |
| 78 | + |
| 79 | +**(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. |
| 80 | + |
| 81 | +> |
| 82 | +```html |
| 83 | +<!-- index.html --> |
| 84 | +<body> |
| 85 | + <div ui-view></div> |
| 86 | + <!-- We'll also add some navigation: --> |
| 87 | + <a ui-sref="state1">State 1</a> |
| 88 | + <a ui-sref="state2">State 2</a> |
| 89 | +</body> |
| 90 | +``` |
| 91 | + |
| 92 | +**(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. |
| 93 | + |
| 94 | +> |
| 95 | +```html |
| 96 | +<!-- partials/state1.html --> |
| 97 | +<h1>State 1</h1> |
| 98 | +<hr/> |
| 99 | +<a ui-sref="state1.list">Show List</a> |
| 100 | +<div ui-view></div> |
| 101 | +``` |
| 102 | +```html |
| 103 | +<!-- partials/state2.html --> |
| 104 | +<h1>State 2</h1> |
| 105 | +<hr/> |
| 106 | +<a ui-sref="state2.list">Show List</a> |
| 107 | +<div ui-view></div> |
| 108 | +``` |
| 109 | + |
| 110 | +**(4)** Next, we'll add some child templates. *These* will get plugged into the `ui-view` of their parent state templates. |
| 111 | + |
| 112 | +> |
| 113 | +```html |
| 114 | +<!-- partials/state1.list.html --> |
| 115 | +<h3>List of State 1 Items</h3> |
| 116 | +<ul> |
| 117 | + <li ng-repeat="item in items">{{ item }}</li> |
| 118 | +</ul> |
| 119 | +``` |
| 120 | + |
| 121 | +> |
| 122 | +```html |
| 123 | +<!-- partials/state2.list.html --> |
| 124 | +<h3>List of State 2 Things</h3> |
| 125 | +<ul> |
| 126 | + <li ng-repeat="thing in things">{{ thing }}</li> |
| 127 | +</ul> |
| 128 | +``` |
| 129 | + |
| 130 | +**(5)** Finally, we'll wire it all up with `$stateProvider`. Set up your states in the module config, as in the following: |
| 131 | + |
| 132 | + |
| 133 | +> |
| 134 | +```javascript |
| 135 | +myApp.config(function($stateProvider, $urlRouterProvider) { |
| 136 | + // |
| 137 | + // For any unmatched url, redirect to /state1 |
| 138 | + $urlRouterProvider.otherwise("/state1"); |
| 139 | + // |
| 140 | + // Now set up the states |
| 141 | + $stateProvider |
| 142 | + .state('state1', { |
| 143 | + url: "/state1", |
| 144 | + templateUrl: "partials/state1.html" |
| 145 | + }) |
| 146 | + .state('state1.list', { |
| 147 | + url: "/list", |
| 148 | + templateUrl: "partials/state1.list.html", |
| 149 | + controller: function($scope) { |
| 150 | + $scope.items = ["A", "List", "Of", "Items"]; |
| 151 | + } |
| 152 | + }) |
| 153 | + .state('state2', { |
| 154 | + url: "/state2", |
| 155 | + templateUrl: "partials/state2.html" |
| 156 | + }) |
| 157 | + .state('state2.list', { |
| 158 | + url: "/list", |
| 159 | + templateUrl: "partials/state2.list.html", |
| 160 | + controller: function($scope) { |
| 161 | + $scope.things = ["A", "Set", "Of", "Things"]; |
| 162 | + } |
| 163 | + }); |
| 164 | +}); |
| 165 | +``` |
| 166 | + |
| 167 | +**(6)** See this quick start example in action. |
| 168 | +>**[Go to Quick Start Plunker for Nested States & Views](http://plnkr.co/edit/u18KQc?p=preview)** |
| 169 | +
|
| 170 | +**(7)** This only scratches the surface |
| 171 | +>**[Dive Deeper!](https://github.com/angular-ui/ui-router/wiki)** |
| 172 | +
|
| 173 | + |
| 174 | +### [Multiple & Named Views](http://plnkr.co/edit/SDOcGS?p=preview) |
| 175 | + |
| 176 | +Another great feature is the ability to have multiple `ui-view`s view per template. |
| 177 | + |
| 178 | +**Pro Tip:** *While multiple parallel views are a powerful feature, you'll often be able to manage your |
| 179 | +interfaces more effectively by nesting your views, and pairing those views with nested states.* |
| 180 | + |
| 181 | +**(1)** Follow the [setup](#get-started) instructions detailed above. |
| 182 | + |
| 183 | +**(2)** Add one or more `ui-view` to your app, give them names. |
| 184 | +> |
| 185 | +```html |
| 186 | +<!-- index.html --> |
| 187 | +<body> |
| 188 | + <div ui-view="viewA"></div> |
| 189 | + <div ui-view="viewB"></div> |
| 190 | + <!-- Also a way to navigate --> |
| 191 | + <a ui-sref="route1">Route 1</a> |
| 192 | + <a ui-sref="route2">Route 2</a> |
| 193 | +</body> |
| 194 | +``` |
| 195 | + |
| 196 | +**(3)** Set up your states in the module config: |
| 197 | +> |
| 198 | +```javascript |
| 199 | +myApp.config(function($stateProvider) { |
| 200 | + $stateProvider |
| 201 | + .state('index', { |
| 202 | + url: "", |
| 203 | + views: { |
| 204 | + "viewA": { template: "index.viewA" }, |
| 205 | + "viewB": { template: "index.viewB" } |
| 206 | + } |
| 207 | + }) |
| 208 | + .state('route1', { |
| 209 | + url: "/route1", |
| 210 | + views: { |
| 211 | + "viewA": { template: "route1.viewA" }, |
| 212 | + "viewB": { template: "route1.viewB" } |
| 213 | + } |
| 214 | + }) |
| 215 | + .state('route2', { |
| 216 | + url: "/route2", |
| 217 | + views: { |
| 218 | + "viewA": { template: "route2.viewA" }, |
| 219 | + "viewB": { template: "route2.viewB" } |
| 220 | + } |
| 221 | + }) |
| 222 | +}); |
| 223 | +``` |
| 224 | + |
| 225 | +**(4)** See this quick start example in action. |
| 226 | +>**[Go to Quick Start Plunker for Multiple & Named Views](http://plnkr.co/edit/SDOcGS?p=preview)** |
| 227 | +
|
| 228 | + |
| 229 | +## Resources |
| 230 | + |
| 231 | +* [In-Depth Guide](https://github.com/angular-ui/ui-router/wiki) |
| 232 | +* [API Reference](http://angular-ui.github.io/ui-router/site) |
| 233 | +* [Sample App](http://angular-ui.github.com/ui-router/sample/) ([Source](https://github.com/angular-ui/ui-router/tree/gh-pages/sample)) |
| 234 | +* [FAQ](https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions) |
| 235 | +* [Slides comparing ngRoute to ui-router](http://slid.es/timkindberg/ui-router#/) |
| 236 | +* [UI-Router Extras / Addons](http://christopherthielen.github.io/ui-router-extras/#/home) (@christopherthielen) |
| 237 | + |
| 238 | +### Videos |
| 239 | + |
| 240 | +* [Introduction Video](https://egghead.io/lessons/angularjs-introduction-ui-router) (egghead.io) |
| 241 | +* [Tim Kindberg on Angular UI-Router](https://www.youtube.com/watch?v=lBqiZSemrqg) |
| 242 | +* [Activating States](https://egghead.io/lessons/angularjs-ui-router-activating-states) (egghead.io) |
| 243 | +* [Learn Angular.js using UI-Router](http://youtu.be/QETUuZ27N0w) (LearnCode.academy) |
| 244 | + |
| 245 | + |
| 246 | + |
| 247 | +## Reporting issues and Contributing |
| 248 | + |
| 249 | +Please read our [Contributor guidelines](CONTRIBUTING.md) before reporting an issue or creating a pull request. |
0 commit comments