Search code examples
javascriptangularjsarraysangular-directive

How do i iterate this object of array using ng-repeat


I have this following object of array returned in my console now what i want to do is to iterate it over using ng-repeat in my index.html file so how will i do it here is the returned json:-

{body: ["xx", "x", "x", "vinit↵", "gyghg", "hjhghg", "j", "jj", "bn", "ss"]}

My app.js:-

var app = angular.module('myApp',[]);
app.controller('movie',['$http','$scope',function($http,$scope) {
    $scope.products = [];
    $http.get('blog/').then(successCallback, errorCallback);
    function successCallback(response){
        products=response.data;
        console.log(products);
    }
    function errorCallback(error){
        console.log(error);
    }

    // body...
}]);

And here what i am doing to iterate those objects in my index.html:-

<div ng-repeat="item in products">
    <ul>
        <li>{{item.body}}</li>
    </ul>
</div>

But Its not printing anything on the screen what i want is to print those array element in that li tag.


Solution

  • Try this

    <!DOCTYPE html>
    <html>
    <head>
      <title></title>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
      <script type="text/javascript">
        angular.module("app", []).controller('Controller', Controller);
    
        function Controller($scope, $http) {
          var res =  {body: ["xx", "x", "x", "vinit↵", "gyghg", "hjhghg", "j", "jj", "bn", "ss"]};
          $scope.products = res.body;
        }
    
      </script>
    </head>
    <body>
    
      <div ng-app="app" ng-controller="Controller" class="container">
        <div ng-repeat="item in products track by $index">
          <ul>
            <li>{{item}}</li>
          </ul>
        </div>
      </div>
    </body>
    </html>