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

时间:2016-01-03 07:05:24

标签: javascript ajax polymer polymer-1.0

                    <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?如果没有,是否有其他解决方案来访问聚合物的响应?谢谢!

1 个答案:

答案 0 :(得分:1)

您必须在{{response.players}}中使用{{response}}代替dom-repeat。这是一个有效的演示。

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;