0

How can I pass array Json data from angularjs Controller to html.

Here is my html

<body  ng-app="bookApp">
<div ng-controller="bookListCtr">
<table>
    <thead>
    <tr>
        <th>something</th>
        <th>something</th>
    </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in items">
            <td><( item.id )></td>
        </tr>
    </tbody>
</table>
</div>

</body>

Here is my Angularjs

var bookApp = angular.module('bookApp', []);
bookApp.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('<(');
    $interpolateProvider.endSymbol(')>');
});

bookApp.controller('bookListCtr', function ($scope, $http) {

    $http.get('http://localhost/client_side/public/book').success(function(data) {
        if(data.s_respond === 200){
            $scope.items = data.data;
            console.log(data.data)
        }

    });
});

This is Json data After console

s_respond = 200
data = "[{"id":"7","title":"Seven is my lucky number","link":"/api/v1/items/7"},{"id":"8","title":"A Dance with Dragons","link":"/api/v1/items/8"},{"id":"10","title":"Ten ways to a better mind","link":"/api/v1/items/10"},{"id":"42","title":"The Hitch-hikers Guide to the Galaxy","link":"/api/v1/items/42"},{"id":"200","title":"Book title #200","link":"/api/v1/items/200"},{"id":"201","title":"Book title #201","link":"/api/v1/items/201"},{"id":"202","title":"Book title #202","link":"/api/v1/items/202"},{"id":"203","title":"Book title #203","link":"/api/v1/items/203"},{"id":"204","title":"Book title #204","link":"/api/v1/items/204"},{"id":"205","title":"Book title #205","link":"/api/v1/items/205"}]"
1
  • your http request gives you response as a string, firstly you should parse data to json object then set foreach loop on json data array object and push every single data object to $scope.items array ! Commented May 10, 2016 at 17:42

2 Answers 2

2

I think that you need parse the json

$scope.items = JSON.parse(data.data);

a link that explain that:

https://www.quora.com/How-can-I-convert-JSON-format-string-into-a-real-object-in-JS

Sign up to request clarification or add additional context in comments.

Comments

0

There r two tags... meaning 2 column try adding another in Ua body

    <body ng-app="bookApp"> <div ng-controller="bookListCtr"> 
<table> 
<thead>
 <tr> <th>something</th> <th>something</th> 
</tr> 
</thead> 
<tbody> 
<tr ng-repeat="item in items">    <td>{{item.id }}</td> 
<td>something else</td>
</tr> 
</tbody> 
</table> 
</div> 
</body>

5 Comments

I just testing with one column friends
use {{ item.id }} expression tag to display your output
I'm using Laravel blade so I have to used another tag to avoid conflict error tage or fantal errors
As you have accepted the answer. You had to parse the string to JSON using JSON.parse() method.
@Arijn Nayak, This is corrected answer. Although I laravel return as Json but when I check out put when I query by ajax in angurlarjs I found that are all string so i have to used this method to convert to Json Data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.