1

I am newbie to angular.js. I am developing a shoppingcart Application, I am getting a JSON Object from database from AJAX call. But I am not getting How to show all the images(url stored in fileLocation in JSON) from JSON response on to Html page using Angular js.

Here is my code:

My JSON

{
  "_embedded": {
    "binaries": [
        {
            "fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
            "username": "testuser3",
            "description": "The company required the 28-year-old's help on a matter the directors felt could affect the share price: its Wikipedia page. Short, uninteresting ."
        },
        {
            "fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
            "username": "Sumanth",
            "description": "Sample"
        },
        {
            "fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
            "username": "as",
            "description": "as"
        }
    ]
  }
}

JSON is assigned to variable data

My Angularjs Controller:

 myAppDirectives.
  controller('MainCtrl', function ($scope,$http,dateFilter) {
  $http({ method: 'GET', url: 'http://localhost:8087/sportsrest/binaries/' }).success(function (data) {
   var result = data;
     var json=JSON.stringify(result);
     var data = JSON.parse(json);
     $scope.cart = data; // response data 
 }).
 error(function (data) {
     alert("error" + data);
     console.log(data);
 });
});

My HTML page:

<html lang="en" ng-app="myApp" id="ng-app">
<body ng-controller="MainCtrl">
  <article class="main-content" role="main">
    <section class="row">
      <div class="content">
        <div class="bookmarks-list">
          <ul>
             <li ng-repeat="data">
                 <h3>{{cart._embedded.binaries.username}}</h3>
                 <img ng-src="{{cart._embedded.binaries.fileLocation}}"/>
             </li>
           </ul>
        </div>
      </div>
    </section>
  </article>    
</body>
</html>

Can Anyone please help me How to iterate through All the images on JSON object and show on HTML page.

Thanks in Advance.

1
  • @ijada thanks for replay. i assigned json stored in data to $scope.cart in my controller code above Commented Mar 2, 2015 at 7:03

2 Answers 2

4

Your ng-repeat is not on the correct variable. It should be on the cart._embedded.binaries array:

<li ng-repeat="item in cart._embedded.binaries">
    <h3>{{item.username}}</h3>
    <img ng-src="{{item.fileLocation}}"/>
 </li>

Also in your controller you probably don't need to parse the data:

$http({ method: 'GET', url: 'http://localhost:8087/sportsrest/binaries/' }).success(function (data) {
     $scope.cart = data; // response data 
})
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for replay.it is not working. Do i need to change anything in controller
Yes, I have updated my answer to illustrate what you might try changing in the success callback of your AJAX request.
@darvin It is showing empty html page. Do i need to assign $scope.item=data; in my controller?
Have you checked for potential errors in the Console?
Inside the success callback if you add console.log(data); can you see the correct JSON printed in the console?
|
1

ngRepeat directive has not been used properly.

This should work for you ..

In the HTML code:

<li ng-repeat="item in cart._embedded.binaries">
             <h3>{{item.username}}</h3>
             <img ng-src="{{item.fileLocation}}"/>
 </li>

Here is a DEMO

5 Comments

@Ashokkumar updated the code with a DEMO. If it's still not working for you, then I would suggest checking your JSON, if its, as you have mentioned in your question
Your demo code differs from the code you used in the answer, this might confuse.
@ijade thanks for replay, i will try it and inform you
@ijade thanks for replay. once i will try and get back to you
@ijade thanks for your support and replay.. your DEMO helped me. Thank you once again. Keep rocking

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.