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: {
'nav@app': { '...' },
'header@app': { '...' }
}
})
// In-app routes
.state('dashboard', {
parent: 'in-app',
url: '/app/dashboard'
views: {
'body@app': { '...' }
}
})
.state('users', {
parent: 'in-app',
url: '/app/users'
views: {
'body@app': { '...' }
}
})
.state('settings', {
parent: 'in-app',
url: '/app/settings'
views: {
'body@app': { '...' }
}
})
At the moment this works great, but for the in-app routes
I would like to define a title that is displayed in the header@app
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.
目前这很好用,但对于应用内路由,我想定义一个标题@ app视图中显示的标题。最好的方法是什么?目前我只能想到在$ rootScope上设置一个变量,或者发送一个事件。但对于这两个我需要一个控制器。
Is there a way I could do this directly from my routes config?
有没有办法直接从我的路线配置中做到这一点?
1 个解决方案
#1
2
The sample applicaiton of the UI-Router, uses this code:
UI-Router的示例应用程序使用以下代码:
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: {
'nav@app': { '...' },
'header@app': { '...' }
}
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
我们可以将状态和参数放入$ rootScope中,这样我们就可以在没有任何控制器的情况下访问它。
我们可以通过数据声明更多自定义内容并将其用作标题... anyhwere
#1
2
The sample applicaiton of the UI-Router, uses this code:
UI-Router的示例应用程序使用以下代码:
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: {
'nav@app': { '...' },
'header@app': { '...' }
}
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
我们可以将状态和参数放入$ rootScope中,这样我们就可以在没有任何控制器的情况下访问它。
我们可以通过数据声明更多自定义内容并将其用作标题... anyhwere