We are no longer accepting contributions to Documentation. Please see our post on meta.

AngularJS

ui-router All Versions

0.9.0
0.9.1
0.9.2
0.9.3
0.9.4
0.9.5
0.9.6
0.9.7
0.9.9
0.9.10
0.9.11
0.9.12
0.9.13
0.9.14
0.9.15
0.9.16
0.9.17
0.9.18
0.9.19
0.10.0
0.10.1
0.10.2
0.10.3
0.10.4
0.10.5
0.10.6
1.0.0rc1
g3-v1.0.0rc1
g3-v1.0.0-rc2
v1.0.0rc2
v1.0.0rc3
v1.0.0rc4
v1.0.0rc5
v1.0.0rc6
v1.0.0rc7
v1.0.0rc8
v1.0.0rc9
v1.0.0rc10
v1.0.0rc11
v1.0.0rc12
1.0.0
1.0.1
1.0.2
1.1.0
1.0.3
1.1.1
1.0.4
1.1.2
1.0.5
1.1.3
1.0.6
1.1.4
1.0.7
1.1.5
1.2.0rc1
1.0.8
1.2.0-rc.2
1.2.0-rc.3
1.2.0
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.6
1.2.7
1.2.8
1.2.9
1.2.10
1.2.11
1.2.12
1.2.13
1.2.14
1.3.0-beta.1
1.3.0-beta.2
1.3.0-beta.3
1.2.15
1.3.0-beta.4
1.2.16
1.3.0-beta.5
1.3.0-beta.6
1.3.0-beta.7
1.3.0-beta.8
1.3.0-beta.9
1.3.0-beta.10
1.2.17
1.3.0-beta.11
1.2.18
1.3.0-beta.12
1.3.0-beta.13
1.2.19
1.3.0-beta.14
1.2.20
1.3.0-beta.15
1.3.0-beta.16
1.2.21
1.3.0-beta.17
1.2.22
1.3.0-beta.18
1.2.23
1.3.0-beta.19
1.3.0-rc.0
1.2.24
1.3.0-rc.1
1.2.25
1.3.0-rc.2
1.3.0-rc.3
1.3.0-rc.4
1.2.26
1.3.0-rc.5
1.3.0
1.3.1
1.3.2
1.3.3
1.2.27
1.3.4
1.3.5
1.3.6
1.3.7
1.2.28
1.3.8
1.4.0-beta.0
1.3.9
1.3.10
1.4.0-beta.1
1.3.11
1.4.0-beta.2
1.3.12
1.4.0-beta.3
1.3.13
1.4.0-beta.4
1.3.14
1.4.0-beta.5
1.3.15
1.4.0-beta.6
1.4.0-rc.0
1.4.0-rc.1
1.4.0-rc.2
1.4.0
1.3.16
1.4.1
1.3.17
1.4.2
1.4.3
1.4.4
1.3.18
1.4.5
1.3.19
1.4.6
1.5.0-beta.0
1.2.29
1.3.20
1.4.7
1.5.0-beta.1
1.5.0-beta.2
1.4.8
1.5.0-rc.0
1.5.0-rc.1
1.4.9
1.5.0-rc.2
1.5.0
1.4.10
1.5.1
1.5.2
1.5.3
1.5.4
1.5.5
1.4.11
1.5.6
1.4.12
1.5.7
1.2.30
1.5.8
1.2.31
1.4.13
1.2.32
1.6.0-rc.0
1.6.0-rc.1
1.5.9
1.6.0-rc.2
1.6.0
1.5.10
1.6.1
1.5.11
1.6.2
1.6.3
1.6.4
1.6.5

This draft deletes the entire topic.

Examples

  • 3

    app.js

    angular.module('myApp', ['ui.router'])
      .controller('controllerOne', function() {
        this.message = 'Hello world from Controller One!';
      })
      .controller('controllerTwo', function() {
        this.message = 'Hello world from Controller Two!';
      })
      .controller('controllerThree', function() {
        this.message = 'Hello world from Controller Three!';
      })
      .config(function($stateProvider, $urlRouterProvider) {
        $stateProvider
          .state('one', {
            url: "/one",
            templateUrl: "view-one.html",
            controller: 'controllerOne',
            controllerAs: 'ctrlOne'
          })
          .state('two', {
            url: "/two",
            templateUrl: "view-two.html",
            controller: 'controllerTwo',
            controllerAs: 'ctrlTwo'
          })
          .state('three', {
            url: "/three",
            templateUrl: "view-three.html",
            controller: 'controllerThree',
            controllerAs: 'ctrlThree'
          });
    
          $urlRouterProvider.otherwise('/one');
      });
    

    index.html

    <div ng-app="myApp">
      <nav>
        <!-- links to switch routes -->
        <a ui-sref="one">View One</a>
        <a ui-sref="two">View Two</a>
        <a ui-sref="three">View Three</a>
      </nav>
      <!-- views will be injected here -->
      <div ui-view></div>
      <!-- templates can live in normal html files -->
      <script type="text/ng-template" id="view-one.html">
        <h1>{{ctrlOne.message}}</h1>
      </script>
    
      <script type="text/ng-template" id="view-two.html">
        <h1>{{ctrlTwo.message}}</h1>
      </script>
    
      <script type="text/ng-template" id="view-three.html">
        <h1>{{ctrlThree.message}}</h1>
      </script>
    </div>
    
  • 3

    app.js

    angular.module('myApp', ['ui.router'])
      .controller('controllerOne', function() {
        this.message = 'Hello world from Controller One!';
      })
      .controller('controllerTwo', function() {
        this.message = 'Hello world from Controller Two!';
      })
      .controller('controllerThree', function() {
        this.message = 'Hello world from Controller Three!';
      })
      .controller('controllerFour', function() {
        this.message = 'Hello world from Controller Four!';
      })
      .config(function($stateProvider, $urlRouterProvider) {
        $stateProvider
          .state('one', {
            url: "/one",
            views: {
              "viewA": {
                templateUrl: "view-one.html",
                controller: 'controllerOne',
                controllerAs: 'ctrlOne'
              },
              "viewB": {
                templateUrl: "view-two.html",
                controller: 'controllerTwo',
                controllerAs: 'ctrlTwo'
              }
            }
          })
          .state('two', {
            url: "/two",
            views: {
              "viewA": {
                templateUrl: "view-three.html",
                controller: 'controllerThree',
                controllerAs: 'ctrlThree'
              },
              "viewB": {
                templateUrl: "view-four.html",
                controller: 'controllerFour',
                controllerAs: 'ctrlFour'
              }
            }
          });
    
        $urlRouterProvider.otherwise('/one');
      });
    

    index.html

    <div ng-app="myApp">
      <nav>
        <!-- links to switch routes -->
        <a ui-sref="one">Route One</a>
        <a ui-sref="two">Route Two</a>
      </nav>
      <!-- views will be injected here -->
      <div ui-view="viewA"></div>
      <div ui-view="viewB"></div>
      <!-- templates can live in normal html files -->
      <script type="text/ng-template" id="view-one.html">
        <h1>{{ctrlOne.message}}</h1>
      </script>
    
      <script type="text/ng-template" id="view-two.html">
        <h1>{{ctrlTwo.message}}</h1>
      </script>
    
      <script type="text/ng-template" id="view-three.html">
        <h1>{{ctrlThree.message}}</h1>
      </script>
    
      <script type="text/ng-template" id="view-four.html">
        <h1>{{ctrlFour.message}}</h1>
      </script>
    </div>
    
  • 2

    app.js

    angular.module('myApp', ['ui.router'])
      .service('User', ['$http', function User ($http) {
        this.getProfile = function (id) {
          return $http.get(...) // method to load data from API
        };
      }])
      .controller('profileCtrl', ['profile', function profileCtrl (profile) {
        // inject resolved data under the name of the resolve function
        // data will already be returned and processed
        this.profile = profile;
      }])
      .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider
          .state('profile', {
            url: "/profile/:userId",
            templateUrl: "profile.html",
            controller: 'profileCtrl',
            controllerAs: 'vm',
            resolve: {
              profile: ['$stateParams', 'User', function ($stateParams, User) {
                // $stateParams will contain any parameter defined in your url
                return User.getProfile($stateParams.userId)
                  // .then is only necessary if you need to process returned data
                  .then(function (data) {
                    return doSomeProcessing(data);
                  });
              }]
            }
          }]);
    
          $urlRouterProvider.otherwise('/');
      });
    

    profile.html

    <ul>
      <li>Name: {{vm.profile.name}}</li>
      <li>Age: {{vm.profile.age}}</li>
      <li>Sex: {{vm.profile.sex}}</li>
    </ul>
    

    View UI-Router Wiki entry on resolves here.

    Resolve functions must be resolved before the $stateChangeSuccess event is fired, which means that the UI will not load until all resolve functions on the state have finished. This is a great way to ensure that data will be available to your controller and UI. However, you can see that a resolve function should be fast in order to avoid hanging the UI.

  • 1

    app.js

    var app = angular.module('myApp',['ui.router']);
    
    app.config(function($stateProvider,$urlRouterProvider) {
    
        $stateProvider
    
        .state('home', {
            url: '/home',
            templateUrl: 'home.html',
            controller: function($scope){
                $scope.text = 'This is the Home'
            }
        })
    
        .state('home.nested1',{
            url: '/nested1',
            templateUrl:'nested1.html',
            controller: function($scope){
                $scope.text1 = 'This is the nested view 1'
            }
        })
    
        .state('home.nested2',{
            url: '/nested2',
            templateUrl:'nested2.html',
            controller: function($scope){
                $scope.fruits = ['apple','mango','oranges'];
            }
        });
    
        $urlRouterProvider.otherwise('/home');
    
    });
    

    index.html

      <div ui-view></div>
       <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
       <script src="angular-ui-router.min.js"></script>
       <script src="app.js"></script>
    

    home.html

    <div>
    <h1> {{text}} </h1>
    <br>
        <a ui-sref="home.nested1">Show nested1</a>
        <br>
        <a ui-sref="home.nested2">Show nested2</a>
        <br>
    
        <div ui-view></div>
    </div>
    

    nested1.html

    <div>
    <h1> {{text1}} </h1>
    </div>
    

    nested2.html

    <div>
        <ul>
          <li ng-repeat="fruit in fruits">{{ fruit }}</li>
        </ul>    
    </div>
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

What is ui-router?

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 navigates 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.

Useful links

You can find the official API Documentation here. For questions about ui-router VS ng-router, you can find a reasonably detailed answer here. Keep in mind ng-router has already released a new ng-router update called ngNewRouter (compatible with Angular 1.5+/2.0) which supports states just like ui-router. You can read more about ngNewRouter here.

Still have a question about ui-router? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.