0
votes

I have some problem with angularJS I can't display json data into my html. my code is this :

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!"); 
        });

});

and my HTML is this :

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

but I have this error : Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: post in posts, Duplicate key: string:[, Duplicate value: "["

How can I display data?

1

1 Answers

0
votes

Change

ng-repeat="post in posts"

to

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

EDIT:

After looking at your data, I realized it is actually an array of objects inside an array. Convert that into just an array of objects.

Alternatively, if you don't have access to the API, change:

$scope.posts = JSON.stringify(data) 

to

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