不允许在转发器中重复

时间:2015-10-23 09:00:07

标签: javascript json angularjs cordova

我对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>    

但我有这个错误: 不允许在转发器中重复。使用&#39;跟踪&#39;表达式以指定唯一键。转发器:在帖子中发帖,重复键:字符串:[,重复值:&#34; [&#34;

如何显示数据?

1 个答案:

答案 0 :(得分:0)

更改

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.