【问题标题】:In polymer-dart how do I count inside a repeating template在聚合物飞镖中,我如何在重复模板中计数
【发布时间】:2013-08-12 12:05:57
【问题描述】:

假设我有一个重复模板:

<template bind repeat id='my-template'>
    This is the bound value: <span id="#myid[x]"> {{}} </span>
</template>

我可以用什么来替换 [x] ?访问循环计数器可以解决问题,但我愿意接受建议。

【问题讨论】:

    标签: templates dart polymer dart-polymer


    【解决方案1】:

    我正在向 Fancy Syntax(Polymer.dart 现在的默认绑定语法)添加一些实用程序来帮助解决这个问题,但基本大纲是通过一个过滤器运行您的集合,该过滤器将添加索引并返回一个新的 Iterable。

    下面是一些现在可以执行的代码:

    import 'package:fancy_syntax/fancy_syntax.dart';
    import 'package:mdv/mdv.dart';
    
    Iterable<IndexedValue> enumerate(Iterable iterable) {
      int i = 0;
      return iterable.map((e) => new IndexedValue(i++, e));
    }
    
    class IndexedValue<V> {
      final int index;
      final V value;
    
      IndexedValue(this.index, this.value);
    }
    
    main() {
      query('#my-template')
        ..bindingDelegate = new FancySyntax(globals: {
          'enumerate': enumerate,
        })
        ..model = ['A', 'B', 'C'];
    }
    
    <template bind id='my-template'>
      <template repeat="{{ item in this | enumerate }}">
        This is the bound value: <span id="#myid-{{ item.index }}">{{ item.value }}</span>
      </template>
    </template>
    

    我正在尝试将一堆实用程序(例如 Python 的 itertools)放入一个库中,用于此类用途。有空我会更新。

    【讨论】:

      猜你喜欢
      • 2013-10-26
      • 2014-10-14
      • 2015-05-22
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      相关资源
      最近更新 更多