如何从从<iron-ajax>接收的数据创建重复元素

How can I create a repeating element from data received from <iron-ajax>?

本文关键字:数据 创建 元素 iron-ajax      更新时间:2023-09-26
                    <iron-ajax auto
                           url='http://api.fantasy.nfl.com/v1/players/stats'
                           handle-as="json"
                           last-response="{{response}}"></iron-ajax>

                <template is="dom-repeat" items="{{response}}">
                    <paper-material class="add-players">
                        <div class="layout horizontal center">
                            <h2>{{item.players.name}}</h2> //NOT SURE WHAT SYNTAX SHOULD BE
                        </div>
                    </paper-material>
                </template>

我正在使用从公共 API 返回响应。我遇到的问题是 API 返回一个对象,而 Polymer 不允许我们对对象进行 dom-repeat。我真的在尝试访问该对象中的数组,有没有办法从返回的对象中提取该数组并对该数组进行 dom-repeat?如果没有,是否有另一种解决方案可以访问聚合物的响应?谢谢!

您必须在dom-repeat中使用{{response.players}}而不是{{response}}。这是一个工作演示。

<!DOCTYPE html>
<html>  
<head>
  <title>paper-scroll-header-panel not working</title>
  
  <script src="https://rawgit.com/webcomponents/webcomponentsjs/master/webcomponents.js"></script>
  
  <base href="https://cdn.rawgit.com/download/polymer-cdn/1.0.1/lib/">
  <link rel="import" href="iron-ajax/iron-ajax.html">
  <link rel="import" href="paper-material/paper-material.html">
  
  
  
  
  <!--<link rel="import" href="all-elements.html">-->
  
</head>
<body class="fullbleed">
<test-elem></test-elem>
<dom-module id="test-elem">
  <template>
   <iron-ajax auto
                           url='http://api.fantasy.nfl.com/v1/players/stats'
                           handle-as="json"
                           last-response="{{response}}"></iron-ajax>
                <template is="dom-repeat" items="{{response.players}}">
                    <paper-material class="add-players">
                        <div class="layout horizontal center">
                            <h2>{{item.name}}</h2>
                        </div>
                    </paper-material>
                </template>
    </template>
  <script>
    Polymer({
      is : "test-elem"
      });
    </script>
    
</dom-module>






</body>
</html>