Polymer:如何循环并将HTML呈现给屏幕

Hap*_*der 8 polymer

我正在使用一个小部件,使用json从数据库中提取第三方信息.收集信息后,我计划遍历信息并创建所需的HTML代码,并通过{{variable}}将其插入到模板中

现在我得到了意想不到的结果.当我这样做时,html显示为文本.

以下是该问题的一些sudo代码:

       <polymer-element name="loop-element">
            <template>
                  {{customerList}}
            </template>
            <script>
                Polymer('loop-element', {
                   ready: function() {
                       this.loadCustomerList();
                   }
                   customerList:"Loading customer list...",

                   loadCustomerList: function() {
                       CustomerNameArray[] = //Get the array from jSon file
                       i = 0;
                       do {
                           this.customerList = "<div>" + customerNameArray[i] + "</div>";
                       } while (customerNameArray[i]);
                   }

                });

            </script>
        </polymer-element>
Run Code Online (Sandbox Code Playgroud)

基本上DIV不会被渲染,而是以文本形式打印到屏幕上:

"<div>Name 1</div>" "<div>Name 2</div>" ... n
Run Code Online (Sandbox Code Playgroud)

代替:

Name 1
Name 2
Name n...
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到一个JSBin示例:http://jsbin.com/tituzibu/1/edit

任何人都可以推荐如何输出列表到模板?

Saj*_*kar 12

聚合物v.1.0

/* relative path to polymer.html*/
<link rel="import" href="../bower_components/polymer/polymer.html">

<dom-module id="employee-list">
    <style>
        /* CSS rules for your element */
    </style>
    <template>
        <div> Employee list: </div>
        <template is="dom-repeat" items="{{employees}}">
            <div>First name: <span>{{item.first}}</span></div>
            <div>Last name: <span>{{item.last}}</span></div>
        </template>
    </template>
</dom-module>

<script>
    Polymer({
        is: 'employee-list',
        ready: function() {
            this.employees = [
                {first: 'Bob', last: 'Smith'},
                {first: 'Sally', last: 'Johnson'}
            ];
        }
    });
</script>
Run Code Online (Sandbox Code Playgroud)

DOC


ebi*_*del 10

您应该使用Polymer的基于DOM的数据绑定功能,而不是自己创建标记.

<template repeat="{{customer, i in customers}}">
  <div>{{i}}, {{customer.name}}</div>
</template>
Run Code Online (Sandbox Code Playgroud)

http://www.polymer-project.org/docs/polymer/databinding.html#an-example

  • 好吧,如果Polymer在CodeCademy.com上,这将是一个很好的开始.教学方法非常简单,有效,而且效果很好.绝对是一个术语词汇表会很棒,有一半时间我不知道该怎么称呼{{thingy}}或指出"指令部分"或理解事物加载的顺序......到目前为止它已经被试用了错误(但很有趣).我仍然不知道如何使用Polymer-Project.com网站:)我通常谷歌:"术语+聚合物项目",并希望它在网站上找到正确的页面.但我真正喜欢的是一系列简单到复杂的教程. (2认同)