我正在尝试使用 Ember.js 制作一个简单的应用程序。我正在使用yeoman ember
.
我有以下申请模板
<h1>Dashboard</h1>
<div class="content">{{outlet}}</div>
我定义了一些路线。现在我无法做的是,找到一种方法来更新 <h1>
中的文本对于不同的路线。例如,
如果我导航到/#/users
我需要将其更改为 Users
。
如果我导航到/#/users/1
我需要它来更改为模特的名字。
我在ApplicationRoute中尝试过这个
setupController: function(controller){
controller.set('title', 'Dashboard');
}
并更改为<h1>{{title}}</h1>
但它只适用于 ApplicationRoute
没有其他路线!
setupController: function(controller){
controller.set('title', 'Users');
}
这在 UsersRoute 中不起作用。甚至路由的输出也消失了。
让它发挥作用的正确方法是什么!
最佳答案
您只需在 ApplicationRoute 中创建一个操作,将标题设置为作为参数传递给它,然后让您的子路由在 setupController 中调用该操作。这应该够了吧。 所以在你的申请 route :
//in application route
actions:{
updatePageTitle: function(title){
this.controllerFor('application').set('title', title);
}
}
在您的个人子 route :
setupController:function(controller, model){
this.send('updatePageTitle', model.get('something'));
this._super(controller, model);
}
关于javascript - 处理 ember.js 中的页面标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23897837/