聚合物dom重复没有数组

时间:2016-06-29 07:33:03

标签: polymer polymer-1.0

我可以在没有阵列的情况下使用聚合物dom-repeat模板吗? 例如,我想渲染一些代码20次。

   <template is="dom-repeat" items="[[itemCounter]]">
      <div>Whatever</div>
   </template>

这将使<div>Whatever</div>呈现20次,但要实现这一点,我必须创建一个数组&#34; itemCounter&#34;在长度为20的组件属性中,唯一的目的是循环它。

我想知道这样的事情是否可行,所以我不需要创建数组。

   <template is="dom-repeat" times="20">
      <div>Whatever</div>
   </template>

2 个答案:

答案 0 :(得分:2)

不,你不能用正常的dom-repeat来做这件事,但是我写了一个完全解决问题的组件:https://github.com/MeTaNoV/dom-repeat-n

您可以在此处找到Polymer Github存储库中有关此功能的讨论:https://github.com/Polymer/polymer/issues/3313

答案 1 :(得分:0)

你可以像这样做一个混乱的黑客

<强>属性:

totalNo: {
  type: Number,
  value: 20
},
_arrayContainer: {
  type: Array ,
  value: [],
  computed: '_numberToArray(totalNo)'
}

方式:

_numberToArray: function(totalNo) {
  var array = [], i;
  for (i = 0; i < totalNo; ++i) {
    array.push(i);
  };
  return array;
},

<强> HTML:

<template is="dom-repeat" items="[[_arrayContainer]]">
  <div>Whatever</div>
</template>

但我不确定你是否真的想要。