1

In my index.html file we use two ng-repeat file to get data from data.json .

In this approach each data created repeated DOM.

How to use single ng-repeat instead of double ng-repeat in my index.html .

<div ng-repeat="x in userdata">
      <item data="y" ng-repeat="y in x.content"></item>
    </div>

https://plnkr.co/edit/uGRmYSv90kbjFzx9qmkI?p=preview

7
  • how exactly you want the output to be? Commented Nov 21, 2016 at 10:08
  • I want to get each object in single ng-repeat but my code get each results use one ng-repeat Commented Nov 21, 2016 at 10:10
  • @jos: I want to use single ng-repeat to get same results Commented Nov 21, 2016 at 10:12
  • @CodeMan You need to prepare your data, instead of userdata it should a concatenation of all content array imo Commented Nov 21, 2016 at 10:12
  • @codeMan your content array can be conveted to a map where name is the key and content as the value. add this map to array and repeat it Commented Nov 21, 2016 at 10:14

3 Answers 3

1

you can use nested angular for each in js,so u can use single ng-repeat using final scope

Database.getDatabase().success(function(data){
      $scope.userdata=data.document;
       $scope.sample = [];
  angular.forEach($scope.userdata, function(content) {
    angular.forEach(content.content, function(content) {
      $scope.sample.push(content);
    })
  })

}).error(function(){
});

now u can loop with sample as

<div ng-repeat="x in sample">
     {{x.content}}
    </div>

updated with foreach

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

1 Comment

i have only two "content" array objects avilable in "document" so i need to repeat two times only .Each ng-repeat i need to get all the details liks "content" and "name"
0

Instead of doing $scope.userdata = data.document you could do:

$scope.userdata = data.document.reduce(function(a, b) { 
    return a.concat(b.content);
}, []);

This will place all the content in the same array. and use it exactly how you have commented in your plunker

<item data="x" ng-repeat="x in userdata"></item>

Here's an update on your plnkr

1 Comment

slim one compare to mine
0

create a map with the contents and add as another object to the documents

var createMap = function(list) {
   var map = {};
   for(var i =0;i <list.length ; i++) {
     map[list[i].name] = list[i].content;
  }
   return map;
 };
    Database.getDatabase().success(function(data){
      for(var i =0;i <data.document.length ; i++) {
        data.document[i]['userdata'] = createMap(data.document[i].content);
      }
      $scope.userdata=data.document;
      console.log($scope.userdata);
}).error(function(){
});

plnkr here

Comments

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.