Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS nested routes example questions
    primarykey
    data
    text
    <p>Any explanation why the sample code (ui-router/sample/index.html) for angular-ui-router (<a href="https://github.com/angular-ui/ui-router" rel="nofollow">https://github.com/angular-ui/ui-router</a>) looks like this. Specifically:</p> <ol> <li>Why the nested definitions of objects like controllers?</li> <li><p>Why the specification of dependencies like this: </p> <p>angular.module('sample', ['ui.compat']) .config( [ '$stateProvider', '$routeProvider', '$urlRouterProvider', function ($stateProvider, $routeProvider, $urlRouterProvider) {</p></li> </ol> <p>thanks</p> <pre><code>&lt;!doctype html&gt; &lt;html lang="en" ng-app="sample"&gt;&lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;link rel="stylesheet" type="text/css" href="bootstrap.min.css"&gt; &lt;style type="text/css"&gt; .fade-enter-setup, .fade-leave-setup { transition: opacity 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0s; } .fade-enter-setup, .fade-leave-setup.fade-leave-start { opacity: 0; } .fade-leave-setup, .fade-enter-setup.fade-enter-start { opacity: 1; } &lt;/style&gt; &lt;script src="../lib/angular-1.1.4.js"&gt;&lt;/script&gt; &lt;script src="../build/angular-ui-router.js"&gt;&lt;/script&gt; &lt;!-- could easily use a custom property of the state here instead of 'name' --&gt; &lt;title ng-bind="$state.current.name + ' - ui-router'"&gt;ui-router&lt;/title&gt; &lt;/head&gt;&lt;body&gt; &lt;div class="navbar navbar-fixed-top"&gt; &lt;div class="navbar-inner"&gt;&lt;div class="container"&gt; &lt;a class="brand" href="#"&gt;ui-router&lt;/a&gt; &lt;ul class="nav"&gt; &lt;li ng-class="{ active: $state.includes('contacts') }"&gt;&lt;a href="#/contacts"&gt;Contacts&lt;/a&gt;&lt;/li&gt; &lt;li ng-class="{ active: $state.includes('about') }"&gt;&lt;a href="#/about"&gt;About&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;p class="navbar-text pull-right" ui-view="hint"&gt;&lt;/p&gt; &lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="container" style="margin-top:60px" ui-view ng-animate="{enter:'fade-enter'}"&gt;&lt;/div&gt; &lt;hr&gt; &lt;pre&gt; $state = {{$state.current.name}} $stateParams = {{$stateParams}} &lt;/pre&gt; &lt;/body&gt;&lt;script&gt; function findById(a, id) { for (var i=0; i&lt;a.length; i++) { if (a[i].id == id) return a[i]; } } angular.module('sample', ['ui.compat']) .config( [ '$stateProvider', '$routeProvider', '$urlRouterProvider', function ($stateProvider, $routeProvider, $urlRouterProvider) { $urlRouterProvider .when('/c?id', '/contacts/:id') .otherwise('/'); $routeProvider .when('/user/:id', { redirectTo: '/contacts/:id', }) .when('/', { template: '&lt;p class="lead"&gt;Welcome to the ngStates sample&lt;/p&gt;&lt;p&gt;Use the menu above to navigate&lt;/p&gt;' + '&lt;p&gt;Look at &lt;a href="#/c?id=1"&gt;Alice&lt;/a&gt; or &lt;a href="#/user/42"&gt;Bob&lt;/a&gt; to see a URL with a redirect in action.&lt;/p&gt;', }); $stateProvider .state('contacts', { url: '/contacts', abstract: true, templateUrl: 'contacts.html', controller: [ '$scope', '$state', function ($scope, $state) { $scope.contacts = [{ id: 1, name: "Alice", items: [{ id: 'a', type: 'phone number', value: '555-1234-1234', },{ id: 'b', type: 'email', value: 'alice@mailinator.com', }], }, { id: 42, name: "Bob", items: [{ id: 'a', type: 'blog', value: 'http://bob.blogger.com', },{ id: 'b', type: 'fax', value: '555-999-9999', }], }, { id: 123, name: "Eve", items: [{ id: 'a', type: 'full name', value: 'Eve Adamsdottir', }], }]; $scope.goToRandom = function () { var contacts = $scope.contacts, id; do { id = contacts[Math.floor(contacts.length * Math.random())].id; } while (id == $state.params.contactId); $state.transitionTo('contacts.detail', { contactId: id }); }; }], }) .state('contacts.list', { // parent: 'contacts', url: '', templateUrl: 'contacts.list.html', }) .state('contacts.detail', { // parent: 'contacts', url: '/{contactId}', resolve: { something: [ '$timeout', '$stateParams', function ($timeout, $stateParams) { return $timeout(function () { return "Asynchronously resolved data (" + $stateParams.contactId + ")" }, 10); }], }, views: { '': { templateUrl: 'contacts.detail.html', controller: [ '$scope', '$stateParams', 'something', function ($scope, $stateParams, something) { $scope.something = something; $scope.contact = findById($scope.contacts, $stateParams.contactId); }], }, 'hint@': { template: 'This is contacts.detail populating the view "hint@"', }, 'menu': { templateProvider: [ '$stateParams', function ($stateParams){ // This is just to demonstrate that $stateParams injection works for templateProvider // $stateParams are the parameters for the new state we're transitioning to, even // though the global '$stateParams' has not been updated yet. return '&lt;hr&gt;&lt;small class="muted"&gt;Contact ID: ' + $stateParams.contactId + '&lt;/small&gt;'; }], }, }, }) .state('contacts.detail.item', { // parent: 'contacts.detail', url: '/item/:itemId', views: { '': { templateUrl: 'contacts.detail.item.html', controller: [ '$scope', '$stateParams', '$state', function ($scope, $stateParams, $state) { $scope.item = findById($scope.contact.items, $stateParams.itemId); $scope.edit = function () { $state.transitionTo('contacts.detail.item.edit', $stateParams); }; }], }, 'hint@': { template: 'Overriding the view "hint@"', }, }, }) .state('contacts.detail.item.edit', { views: { '@contacts.detail': { templateUrl: 'contacts.detail.item.edit.html', controller: [ '$scope', '$stateParams', '$state', function ($scope, $stateParams, $state) { $scope.item = findById($scope.contact.items, $stateParams.itemId); $scope.done = function () { $state.transitionTo('contacts.detail.item', $stateParams); }; }], }, }, }) .state('about', { url: '/about', templateProvider: [ '$timeout', function ($timeout) { return $timeout(function () { return "Hello world" }, 100); }], }) .state('empty', { url: '/empty', templateUrl: 'empty.html', controller: [ '$scope', '$state', function ($scope, $state) { // Using an object to access it via ng-model from child scope $scope.data = { initialViewTitle: "I am an initial view" } $scope.changeInitialViewTitle = function($event) { $state.transitionTo('empty.emptycontent'); }; $scope.showInitialView = function($event) { $state.transitionTo('empty'); }; }] }) .state('empty.emptycontent', { url: '/content', views: { 'emptycontent': { templateUrl: 'empty.content.html' } } }); }]) .run( [ '$rootScope', '$state', '$stateParams', function ($rootScope, $state, $stateParams) { $rootScope.$state = $state; $rootScope.$stateParams = $stateParams; }]); &lt;/script&gt;&lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload