admin管理员组

文章数量:1415139

I have a big object that looks something like this :

scope.marketplaces = {

    first_example : { ... },
    second_example : { ... },

    ...

};

What I'm trying to do is loop through the big object like this :

<section>
    <ul>
        <li ng-repeat="(key, value) in marketplaces"></li>
    </ul>
</section>

And inside the loop, loop again each object, but instead of appending to DOM something like :

<li> ... first level loop ...

    <li> ... second level loop ... </li>
</li>

I would like to have just one <li></li> despite the level I'm looping through. The reason why I want it like that is because I need the key from the first loop to be referenced down the level loops and still have just one li.

I've read that ng-repeat="friend in friends | filter:searchText" could do what I want but I'm not sure if in the filter expression I can dynamically specify the key or something else that is needed instead of searchText ( I guess that is an already know property of the object ).

So my question is, can I achieve what I just explained with the filter or is there another way of doing it ?

I have a big object that looks something like this :

scope.marketplaces = {

    first_example : { ... },
    second_example : { ... },

    ...

};

What I'm trying to do is loop through the big object like this :

<section>
    <ul>
        <li ng-repeat="(key, value) in marketplaces"></li>
    </ul>
</section>

And inside the loop, loop again each object, but instead of appending to DOM something like :

<li> ... first level loop ...

    <li> ... second level loop ... </li>
</li>

I would like to have just one <li></li> despite the level I'm looping through. The reason why I want it like that is because I need the key from the first loop to be referenced down the level loops and still have just one li.

I've read that ng-repeat="friend in friends | filter:searchText" could do what I want but I'm not sure if in the filter expression I can dynamically specify the key or something else that is needed instead of searchText ( I guess that is an already know property of the object ).

So my question is, can I achieve what I just explained with the filter or is there another way of doing it ?

Share Improve this question asked Feb 17, 2013 at 14:16 RolandRoland 9,74119 gold badges82 silver badges136 bronze badges 2
  • Can you write example even not working html markup what you would like to have? – Valentyn Shybanov Commented Feb 17, 2013 at 15:12
  • 1 sounds like you want to flatten your data before passing it into the controller – charlietfl Commented Feb 18, 2013 at 22:33
Add a ment  | 

1 Answer 1

Reset to default 4

I hope I've understood question: you would like to have one ngRepeat on a nested object. So kind of linearize object.

First approach would be to create filter:

app.filter('linear', function() {
  return function(value) {
    var result = {};
    for(var key in value) {
      for(var cKey in value[key]) {
        result[key+'_'+cKey] = value[key][cKey];
      }      
    }
    return result;
  };
});

and in thml:

<li ng-repeat="(key, value) in marketplaces | linear">
          {{key}}: {{value}}
</li>

But unfortunally AngularJS have problems when in filter you create new elements. You can have error message in console kind of:

10 $digest iterations reached

Without hacks with $$hash you can't achieve that functionality for now (Please correct me if I am wrong).

So I think for now the solution would be to watch 'marketplaces' in controller and create new object using same code as in controller that use in ngRepeat:

$scope.$watch('marketplaces', function(value) {
    var result = {};
    for(var key in value) {
      for(var cKey in value[key]) {
        result[key+'_'+cKey] = value[key][cKey];
      }      
    }
    $scope.linearMarketplaces = result;

  }, true);

And in HTML:

    <li ng-repeat="(key, value) in linearMarketplaces">
      {{key}}: {{value}}
    </li>

Plunker with both solutions: http://plnkr.co/edit/pYWFhqSqKAa9gpzek77P?p=preview

本文标签: javascriptangularjs ngrepeat on two levels but just one outputStack Overflow