簡體   English   中英

如何使html屬性的值依賴於Ember.js視圖呈現的項目?

[英]How do I make the html attribute's value dependent on the item being rendered by an Ember.js view?

假設我有這樣的觀點:

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>

我怎么做?

這是使用bindAttr href和ItemsController進行的超快速方法: http//jsfiddle.net/nLa8Z/1/有關更詳細的文檔,請查看http://emberjs.com/documentation/#toc_binding-element-attributes-與-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"})
     ]});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM