如this article所示,您可以定義一個新的陣列,包括各行索引:
App.MyView = Ember.View.extend({
oneArrayWithIndices: function() {
return this.get('oneArray').map(function(item, index) {
return {item: i, index: idx};
});
}.property('[email protected]')
});
,然後在模板中,您可以訪問像這樣的指標:
{{#each view.oneArrayWithIndices}}
index: {{this.index}} <br />
item: {{this.item}}
{{/#each}}
但是,由於您只想顯示數組中的特定項目,因此最好在您的視圖中進行操作(或者在控制器中更好)。 儘量保持您的模板無邏輯。
因此,在創建視圖/控制器的新數組只包括要顯示的項目:
myFilteredArray: function() {
return this.get('oneArray').filter(function(item, index) {
// return true if you want to include this item
// for example, with the code below we include all but the first item
if (index > 0) {
return true;
}
});
}.property('[email protected]')
'{{#如果}}'不能包含一個比較AFAIK。它會檢查一個虛擬變量。即'null','false','undefined'或'[]'。 – albertjan 2012-07-17 07:58:32