Skip to content
The de-facto solution to flexible routing with nested views in AngularJS
TypeScript JavaScript CSS HTML
Failed to load latest commit information.
config feat(view): Route a view to a directive using `component:` Mar 26, 2016
lib Merge feature-1.0 into master Feb 10, 2016
packages fix(ng2.UIRouter): Update ui-router for ng2 rc.1 May 4, 2016
scripts chore(build): fix packaging stateEvents.js into ng1-bower and ng1 Apr 3, 2016
src fix(ng2.UIRouter): Update ui-router for ng2 rc.1 May 4, 2016
test fix(resolve): Fix regression; Allow resolve values to be service names Apr 11, 2016
typedoctheme docs(*): organize docs modules; update some docs to typedoc Apr 3, 2016
typings feat(ui-router-ng2): Initial angular2 support Feb 18, 2016
.gitignore chore(*): create packaging scripts for ng1/ng2/core/ng1-bower Mar 10, 2016
.npmignore prep for 0.2.18 Feb 7, 2016
.travis.yml chore(*): create packaging scripts for ng1/ng2/core/ng1-bower Mar 11, 2016
CHANGELOG.md chore(*): Release prep - Update CHANGELOG Apr 8, 2016
CONTRIBUTING.md docs(repo): updated contributing label images May 20, 2015
Gruntfile.js chore(core): Export all symbols into global namespace for typescript … Mar 26, 2016
LICENSE style(License): update copyright year to range Jan 5, 2015
README.md Update README.md Apr 6, 2016
component.json Merge feature-1.0 into master Feb 10, 2016
files.js feat(ui-router-ng2): Initial angular2 support Feb 19, 2016
package.json chore(*): Pin ng2 dependencies May 4, 2016
tsconfig-ng2.json chore(core): Export all symbols into global namespace for typescript … Mar 26, 2016
tsconfig.json refactor(view): refactor how views are loaded and configured. Mar 13, 2016
tsd.json feat(ui-router-ng2): Initial angular2 support Feb 19, 2016
tslint.json chore(*): tslint resolveContext.ts Sep 3, 2015
tslint.json.sample chore(*): Add tslint dev dependency Aug 24, 2015

README.md

AngularUI Router  Build Status

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 version 0.2.x, it can be found here


The de-facto solution to flexible routing in angular


Docs 1.0.0-alpha.3 | Download stable (or Minified) | Guide | API | Sample (Src) | FAQ | Resources | Report an Issue | Contribute | Help! |


Angular UI-Router is a client-side Single Page Application routing framework for AngularJS.

Routing frameworks for SPAs update the browser's URL as the user nagivates through the app. Conversely, this allows changes to the browser's URL to drive navigation through the app, thus allowing the user to create a bookmark to a location deep within the SPA.

UI-Router applications are modeled as a hierarchical tree of states. UI-Router provides a state machine to manage the transitions between those application states in a transaction-like manner.

Get Started

(1) Get UI-Router in one of the following ways:

  • clone & build this repository
  • download the release (or minified)
  • link to cdn
  • via jspm: by running $ jspm install angular-ui-router from your console
  • or via npm: by running $ npm install angular-ui-router from your console
  • or via Bower: by running $ bower install angular-ui-router from your console
  • or via Component: by running $ component install angular-ui/ui-router from your console

(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)

(3) Add 'ui.router' to your main module's list of dependencies (For Component users: replace 'ui.router' with require('angular-ui-router'))

When you're done, your setup should look similar to the following:

<!doctype html>
<html ng-app="myApp">
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
    <script src="js/angular-ui-router.min.js"></script>
    <script>
        var myApp = angular.module('myApp', ['ui.router']);
        // For Component users, it should look like this:
        // var myApp = angular.module('myApp', [require('angular-ui-router')]);
    </script>
    ...
</head>
<body>
    ...
</body>
</html>

Nested States & Views

The majority of UI-Router's power is in its ability to nest states & views.

(1) First, follow the setup instructions detailed above.

(2) Then, add a ui-view directive to the <body /> of your app.

<!-- index.html -->
<body>
    <div ui-view></div>
    <!-- We'll also add some navigation: -->
    <a ui-sref="state1">State 1</a>
    <a ui-sref="state2">State 2</a>
</body>

(3) You'll notice we also added some links with ui-sref directives. 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.

<!-- partials/state1.html -->
<h1>State 1</h1>
<hr/>
<a ui-sref="state1.list">Show List</a>
<div ui-view></div>
<!-- partials/state2.html -->
<h1>State 2</h1>
<hr/>
<a ui-sref="state2.list">Show List</a>
<div ui-view></div>

(4) Next, we'll add some child templates. These will get plugged into the ui-view of their parent state templates.

<!-- partials/state1.list.html -->
<h3>List of State 1 Items</h3>
<ul>
  <li ng-repeat="item in items">{{ item }}</li>
</ul>
<!-- partials/state2.list.html -->
<h3>List of State 2 Things</h3>
<ul>
  <li ng-repeat="thing in things">{{ thing }}</li>
</ul>

(5) Finally, we'll wire it all up with $stateProvider. Set up your states in the module config, as in the following:

myApp.config(function($stateProvider, $urlRouterProvider) {
  //
  // For any unmatched url, redirect to /state1
  $urlRouterProvider.otherwise("/state1");
  //
  // Now set up the states
  $stateProvider
    .state('state1', {
      url: "/state1",
      templateUrl: "partials/state1.html"
    })
    .state('state1.list', {
      url: "/list",
      templateUrl: "partials/state1.list.html",
      controller: function($scope) {
        $scope.items = ["A", "List", "Of", "Items"];
      }
    })
    .state('state2', {
      url: "/state2",
      templateUrl: "partials/state2.html"
    })
    .state('state2.list', {
      url: "/list",
      templateUrl: "partials/state2.list.html",
      controller: function($scope) {
        $scope.things = ["A", "Set", "Of", "Things"];
      }
    });
});

(6) See this quick start example in action.

Go to Quick Start Plunker for Nested States & Views

(7) This only scratches the surface

Dive Deeper!

Multiple & Named Views

Another great feature is the ability to have multiple ui-views view per template.

Pro Tip: While multiple parallel views are a powerful feature, you'll often be able to manage your interfaces more effectively by nesting your views, and pairing those views with nested states.

(1) Follow the setup instructions detailed above.

(2) Add one or more ui-view to your app, give them names.

<!-- index.html -->
<body>
    <div ui-view="viewA"></div>
    <div ui-view="viewB"></div>
    <!-- Also a way to navigate -->
    <a ui-sref="route1">Route 1</a>
    <a ui-sref="route2">Route 2</a>
</body>

(3) Set up your states in the module config:

myApp.config(function($stateProvider) {
  $stateProvider
    .state('index', {
      url: "",
      views: {
        "viewA": { template: "index.viewA" },
        "viewB": { template: "index.viewB" }
      }
    })
    .state('route1', {
      url: "/route1",
      views: {
        "viewA": { template: "route1.viewA" },
        "viewB": { template: "route1.viewB" }
      }
    })
    .state('route2', {
      url: "/route2",
      views: {
        "viewA": { template: "route2.viewA" },
        "viewB": { template: "route2.viewB" }
      }
    })
});

(4) See this quick start example in action.

Go to Quick Start Plunker for Multiple & Named Views

Resources

Videos

Reporting issues and Contributing

Please read our Contributor guidelines before reporting an issue or creating a pull request.

Something went wrong with that request. Please try again.