Polymer JS – 模板重复对我的数组没有任何作用。 (Chrome)

Polymer JS – template repeat does nothing with my array. (Chrome)

这在我的代码中没有任何作用..

<template repeat="{{item in list}}"> </template>

我也尝试过如下使用,但使用此方法时出现错误:

<template is="dom-repeat" id="example" items="{{list}}"></template>

is="dom-repeat" 出现以下错误:

Uncaught TypeError: Polymer.dom.addDebouncer is not a function
Uncaught TypeError: Polymer.dom is not a function

为什么?

这是我的代码

<link rel="import" href="../lib/iron-ajax/iron-ajax.html">

<link rel="import" href="../lib/polymer/polymer.html">

<link rel="import" href="welcome-title.html">

<dom-module id="remove-user">

<template>
  <iron-ajax id="getAll" url="http://localhost:3002/secure/api/all" method="GET" handle-as="json" on-response="getAllCB" with-credentials='true'></iron-ajax>

    <div class="ui relaxed stackable grid centered" id="admin-container">
        <welcome-title class="ui center aligned row grid"></welcome-title>
        <form class="ui grid remove-user hide twelve wide column" method='post' action="/secure/add-user">
            <h3>Remove User</h3>

            <table class="ui unstackable celled table ">
                <thead>
                    <tr><th class="nine wide">Username</th>
                        <th class="three wide">Permission</th>
                        <th class="one wide"></th>
                    </tr>
                </thead>
                <tbody> ...

这是我感到困惑的地方。我简化了循环以使其更易于调试,但它没有出现。尽管出现了模板重复之外的任何文本。控制台没有错误。

                    <template repeat="{{user in users}}">
                        <span>{{user}}</span>
                    </template>

为什么 repeat 中的内容没有显示在我的页面上?

                    ... <tr>
                        <td><span></span></td>
                        <td></td>
                        <td class="collapsing">
                            <div class="ui fitted checkbox">
                                <input type="checkbox"> <label></label>
                            </div>
                        </td>
                    </tr>

                </tbody>
                <tfoot class="full-width">

                    <tr><th colspan="3">

                        <button class="right floated negative ui button"><i class="remove user icon"></i>Remove User(s)</button>
                    </th>
                </tr></tfoot>
            </table>
        </form>
    </div>

</template>



</dom-module>
<script>

Polymer({

    is: "remove-user",

    ready: function(){

        this.$.getAll.generateRequest();

    },

    getAllCB: function(data){

        this.users = data.detail.response;
    }

});

</script>

当使用 JSON.stringify():

输出到浏览器控制台时,用户 JSON 对象看起来像这样
[{"username":"admin","permission":"admin"},            
{"username":"admin","permission":"application1"},
{"username":"user","permission":"application1"},
{"username":"test","permission":"application1"}]

获取完整项目: 相关文件位于 authentication/public/elements/remove-user.html 加载此元素的主页是 authentication/secure.html https://github.com/CoultonF/Web-Development-3-Node.JS

看起来您正试图在数据绑定中使用表达式,Polymer 1.x 不支持(也不在 2.x 的路线图上)。

这段代码的正确形式:

<template repeat="{{user in users}}">
  <span>{{user}}</span>
</template>

这是:

<template is="dom-repeat" items="{{users}}" as="user">
  <span>{{user}}</span>
</template>

HTMLImports.whenReady(() => {
  Polymer({
    is: 'x-foo',
    properties: {
      users: {
        type: Array,
        value: () => ['Andy', 'Bob', 'Charlie']
      }
    }
  });
});
<head>
  <base href="https://polygit.org/polymer+1.7.1/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="polymer/polymer.html">
</head>
<body>
  <x-foo></x-foo>

  <dom-module id="x-foo">
    <template>
      <template is="dom-repeat" items="{{users}}" as="user">
        <span>{{user}}</span>
      </template>
    </template>
  </dom-module>
</body>

codepen

您可以在 Polymer docs 中阅读有关 dom-repeat 模板的更多信息。