2013-01-19 22 views
2

參考指標,我想用迭代循環的指數在HTML作爲一個值,如:DART的Web UI:如何在迭代循環

<select bind-value="gAddItem.drawType" 
      template iterate="int ind = 0; ind < gDrawDescs.length; ind++"> 
    <option value="{{ind.toString()}}"> {{gDrawDescs[ind]}} </option> 
    </select> 

但這種直觀的語法似乎並不上班。在Dart Web-ui中有沒有辦法做到這一點?

回答

0

飛鏢似乎並沒有允許列表範圍內的文字,這將允許整齊的解決方案:

iterate="ind in [0 ... gDrawDescs.length]" 

要解決這個問題,創建以下功能:

List<int> createList(final int cnt) { 
    var list = new List(); 
    for (var i=0; i<cnt; i++) { 
    list.add(i); 
    } 
    return list; 
} 

而在HTML :

iterate="ind in createList(gDrawDescs.length)" 
+1

這已經接近儘可能好了。 Dart的Web UI庫不公開當前的索引迭代變量,因此您需要創建自己的範圍類型函數來執行此操作。看到這個錯誤:https://github.com/dart-lang/web-ui/issues/277如果您同意這是Web UI應該做的事情,您可以發表評論。 –

0

或者考慮這種

Iterable<int> Indices(int size) => Iterable<int>.generate(size, (i) => i); 
5

好消息,大家好! Seth Ladd在Peter B的回答中提到的問題在幾個月前是fixed,所以現在您可以從<template>中參考{{$index}}。從在該提交中添加的測試代碼中獲得:

Test the $index meta variable: 
<template repeat="item in items" indentation="remove"> 
    <div> 
    "{{item}}" is the {{$index}}{{suffix($index)}} item out of {{$list.length}}. 
    </div> 
</template>