Angular ui-router variable in nested view


I'm currently working on an app where I have multiple nested views, they sort of look like this:

- ui-view 
  - ui-view="header"
  - ui-view="nav"
  - ui-view="body"

My states are defined as follows:

.state('index', {
   url: '', // default route
   templateUrl: 'welcome.html'
})
.state('app', {
    abstract: true,
    templateUrl: 'app.template.html' // This template contains the 3 different ui-views
})

// I'm using a different state here so I can set the navigation and header by default
.state('in-app', { 
    parent: 'app',
    abstract: true,
    views: {
       '[email protected]': { '...' },
       '[email protected]': { '...' }
    }
})

// In-app routes
.state('dashboard', {
     parent: 'in-app',
     url: '/app/dashboard'
     views: {
         '[email protected]': { '...' }
    }
})
.state('users', {
     parent: 'in-app',
     url: '/app/users'
     views: {
         '[email protected]': { '...' }
    }
})
.state('settings', {
     parent: 'in-app',
     url: '/app/settings'
     views: {
         '[email protected]': { '...' }
    }
})

At the moment this works great, but for the in-app routes I would like to define a title that is displayed in the [email protected] view.
What would be the best way to do this? At the moment I can only think of either setting a variable on the $rootScope, or sending out an event. But for both of those I would need a controller.

Is there a way I could do this directly from my routes config?

The sample applicaiton of the UI-Router, uses this code:

ui-router / sample / app / app.js

.run(
[ '$rootScope', '$state', '$stateParams',
    function ($rootScope, $state, $stateParams) {
    // It's very handy to add references to $state and $stateParams to the $rootScope
    // so that you can access them from any scope within your applications.For example,
    // <li ng-class="{ active: $state.includes('contacts.list') }"> will set the <li>
    // to active whenever 'contacts.list' or one of its decendents is active.
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
}])

And that means, that with data : {} feature:

Attach Custom Data to State Objects

You can attach custom data to the state object (we recommend using a data property to avoid conflicts).

// Example shows an object-based state and a string-based state
var contacts = { 
    name: 'contacts',
    templateUrl: 'contacts.html',
    data: {
        customData1: 5,
        customData2: "blue"
    }  
}

we can do this:

.state('in-app', { 
    parent: 'app',
    abstract: true,
    views: {
       '[email protected]': { '...' },
       '[email protected]': { '...' }
    }
    data: { title : "my title" },
})

And use it in some template like:

<div>{{$state.current.data.title}}</div>

Some summary.

  • We can place state and params into $rootScope, so we can access it without any controller anyhwere.
  • We can declare some more custom stuff via data and use it as a title ... anyhwere