【问题标题】:How do I make the html attribute's value dependent on the item being rendered by an Ember.js view?如何使 html 属性的值取决于 Ember.js 视图呈现的项目?
【发布时间】:2012-06-19 23:21:36
【问题描述】:

假设我有这样的看法:

App.AnchorView = Ember.View.extend({
  tagName: 'a',
  attributeBindings: ['href']
});

还有一个类定义:

App.Item = Ember.Object.extend({
  name: "Unknown Entry",
  location: ""
});

还有一个类的实例:

App.Item.create({
   name: "Google",
   location: "http://www.google.com" 
});

我的模板是:

{{#view App.AnchorView}}{{name}}{{/view}}

我希望它呈现为:

<a href="http://www.google.com">Google</a>

我该怎么做?

【问题讨论】:

    标签: templates attributes ember.js attr


    【解决方案1】:

    这是一种使用 bindAttr href 和 ItemsController 的超级快速方法:http://jsfiddle.net/nLa8Z/1/ 如需更详细的文档,请查看http://emberjs.com/documentation/#toc_binding-element-attributes-with-bindattr

    {{#each App.ItemsController}}
         <a {{bindAttr href="location"}}>{{name}}</a>
    {{/each}}
    
    App.ItemsController = Ember.ArrayController.create({
         content: [
             App.Item.create({ name: "Google", location: "http://www.google.com"})
         ]});
    

    【讨论】:

    • 谢谢,但我特别想知道是否可以在视图本身生成的元素上更改它
    猜你喜欢
    • 1970-01-01
    • 2010-12-29
    • 2011-02-26
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    相关资源
    最近更新 更多