【问题标题】:Duplicates in a repeater are not allowed不允许在转发器中重复
【发布时间】:2015-10-23 17:26:23
【问题描述】:

我对 angularJS 有一些问题,我无法将 json 数据显示到我的 html 中。 我的代码是这样的:

app.controller('ServerPostsController', function($scope, $http) {

    $('.loading').show();

    $scope.posts=[];

        $http.get("https://api.basebear.com/json/tables/20a3fb1d-42c7-45cb-9440-2c6d5d10403d/records?apikey=6e954b0a17b74f52b842ec2b0f0d6d0da24ac5ad").
        success(function(data) {


            $scope.posts=JSON.stringify(data);
            console.log("ok" + "data"+  $scope.posts);

        }).
        error(function() {
            $('.loading').hide();
            console.log("no" );

            alert("Si è verificato un errore!"); 
        });

});

我的 HTML 是这样的:

<ons-toolbar fixed-style>
<div class="left">
<ons-toolbar-button onclick="menu.toggleMenu()">
<ons-icon icon="ion-navicon" size="28px" fixed-width="false"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center">Prova</div>
</ons-toolbar> 



<section style="padding: 10px;">

    <ons-row style="padding: 10px 0 20px 0;">
    <input type="search" placeholder="Search" class="search-input" ng-model="search">
    </ons-row>

    <table>
    <tr>
    <th>Id</th>
    <th>Nome</th>
    <th>Cognome</th>
    <th>Data</th>

</tr>
  <tr ng-repeat="post in posts">
    <td>{{post.Value}}</td>
    <td>{{post.Value}}</td>
    <td>{{post.Value}}</td>
    <td>{{post.Value}}</td>

</tr>
</table>
</section>    

但我有这个错误: 不允许在转发器中重复。使用“track by”表达式指定唯一键。中继器:帖子中的帖子,重复键:字符串:[,重复值:“[”

如何显示数据?

【问题讨论】:

    标签: javascript json angularjs cordova


    【解决方案1】:

    改变

    ng-repeat="post in posts"
    

    ng-repeat="post in posts track by $index"
    

    编辑:

    查看您的数据后,我意识到它实际上是数组中的对象数组。将其转换为对象数组。

    或者,如果您无权访问 API,请更改:

    $scope.posts = JSON.stringify(data) 
    

    $scope.posts = angular.toJson(data[0]) //I don't think JSON.stringify is required.
    

    【讨论】:

    • 我这样做了,但我没有看到数据;为什么?
    • 很可能,帖子没有属性“价值”。将 post.Value 更改为仅发布并查看它是否显示任何内容。
    • 你能看到我的 Json 数据吗? api.basebear.com/json/tables/…
    • 你要我看看吗?当然。另外,你做了 console.log("ok" + "data"+ $scope.posts);在你的代码中。它打印什么?
    • 您的数据有问题。数组中有一个对象数组,而它应该只是一个对象数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多