在您的模板中替换li
标签,如下所示:
索引.html
<script type="text/x-handlebars">
<ul class="nav">
{{#linkTo 'ping' tagName="li"}}Ping{{/linkTo}}
{{#linkTo 'pong' tagName="li"}}Pong{{/linkTo}}
</ul>
</script>
当应用程序的{{linkTo}}
当前路由与提供的路由名称匹配时,指定 tagName 的 将自动应用 css 类名称“active”。
例如,当您的应用程序 url 位于/#/ping
结果标记处时,将类似于:
...
<li class="active">Ping</li>
...
或者你创建一个自定义视图
App.ItemView = Ember.View.extend({
tagName: 'li',
classNameBindings: ['active'],
active: function() {
return this.get('childViews.firstObject.active');
}.property()
});
然后像这样使用它
<script type="text/x-handlebars">
<ul class="nav">
{{#view App.ItemView}}
{{#linkTo 'ping'}}Ping{{/linkTo}}
{{/view}}
{{#view App.ItemView}}
{{#linkTo 'pong'}}Pong{{/linkTo}}
{{/view}}
</ul>
</script>
一些CSS来看看它实际工作
li a {
color: #000;
}
li a.active {
color: #f00;
}
希望能帮助到你