首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板不重复?

模板不重复?
EN

Stack Overflow用户
提问于 2015-03-11 02:13:27
回答 1查看 30关注 0票数 0

我有一个数组,它包含一些数据,我需要将这些数据绑定到我创建的自定义元素的不同部分。下面是该元素的相关部分:

代码语言:javascript
复制
<div class="soundcard-container" vertical layout>
    <content select="img"></content>
    <paper-ripple fit></paper-ripple>

    <div class="soundcard-bottom-container" horizontal layout center justified>
        <content class="soundcard-string" select="span"></content>
        <a class="soundcard-download-icon" href="#"></a>
    </div>
</div>

在我的index.html文件中,我试图这样重复它:

代码语言:javascript
复制
<div class="card-container" layout horizontal wrap>
<template repeat="{{s in carddata}}">
    <sound-card>
        <img src="{{s.imgurl}}">
        <span>{{s.quote}}</span>
    </sound-card>
</template>

我的数组相当大,但是这里是精简版本(在我的index.html文件中):

代码语言:javascript
复制
<script>
Polymer({
   ready: function() {
       this.carddata = [
           {imgurl: '../www/img/soundcard-imgs/img1.jpg', quote: 'String one', sound: '../www/card-sounds/sound1.m4a'},
           {imgurl: '../www/img/soundcard-imgs/img2.jpg', quote: 'String two', sound: '../www/card-sounds/sound2.m4a'}
       ];
   }
});
</script>

我是不是搞错了?我以为{{s in carddata}}会对<sound-card>数组中的许多项重复<sound-card>自定义元素?我在聚合物站点上使用了初学者示例,但是当我在http服务器上运行它时,模板永远不会离开display: none。有什么想法吗?或者例子什么的!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-11 13:57:18

只在聚合物元素中起作用。因此,您需要创建一个聚合物元素(例如声卡收集),并将代码从index.html移动到该元素:

元素/声卡-collection.html tion.html

代码语言:javascript
复制
<polymer-element name="sound-card-collection">
  <template>
    <div class="card-container" layout horizontal wrap>
    <template repeat="{{s in carddata}}">
      <sound-card>
        <img src="{{s.imgurl}}">
        <span>{{s.quote}}</span>
      </sound-card>
    </template>
  </template>
  <script>
  Polymer({
     ready: function() {
       this.carddata = [
           {imgurl: '../www/img/soundcard-imgs/img1.jpg', quote: 'String one', sound: '../www/card-sounds/sound1.m4a'},
           {imgurl: '../www/img/soundcard-imgs/img2.jpg', quote: 'String two', sound: '../www/card-sounds/sound2.m4a'}
       ];
   }
  });
  </script>
</polmer-element>

index.html:头部:

代码语言:javascript
复制
<link rel="import" href="elements/sound-card-collection.html">

身体的某处:

代码语言:javascript
复制
<sound-card-collection></sound-card-collection>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28977443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档