0

I have listed items and it's children using ng-repeat.

have items = [
  {item.id:1,
   item.name :'item1',
   item.children: [
      {item.id:11,
       item.name :'child1',  
       },
       {item.id:22,
       item.name :'child2',  
       }, 
       {item.id:33,
       item.name :'child3',  
       }]  
  },
  {item.id:2,
   item.name :'item2',
   item.children: [
      {item.id:12,
       item.name :'child22',  
       },
       {item.id:23,
       item.name :'child33',  
       }, 
       {item.id:34,
       item.name :'child44',  
       }]  
  }
];

<div id = "item.id" ng-repeat="item in vm.items">
  <div>{{item.name}}</div>
  <div id = "child.id" ng-repeat="child in item.children">
    <div>{{child.name}}</div>
  </div>

Than when I receive updated child e.g.

       {item.id:34,
       item.name :'child44_renamed',  
       }

I want to place a child data into element where it was rendered. is there way to do it?

Thank you for advice in advance.

Tome Pejoski
  • 1,582
  • 2
  • 10
  • 34
Serhiy
  • 1,893
  • 9
  • 30
  • 48

1 Answers1

1

Yes you can change the structure of your items array object and Angular will automatically re-render the whole thing.

The easiest way to manipulate with JavaScript objects and arrays is by using a library, such as lodash. You can also do it with plain JavaScript but using a library will save you much time and energy.

By using lodash, for example, you can find the item based on the id and name and then do the changes on it.

Tome Pejoski
  • 1,582
  • 2
  • 10
  • 34
  • thanks. but is it possible to change array directly in html element where it was rendered? – Serhiy Dec 26 '16 at 08:44
  • Why you need to do it that way? Don't want to re-render? – Tome Pejoski Dec 26 '16 at 08:45
  • imagine if child has also children, and it's children has also children. how to get child.array , change it and put back in that place where it was rendered? – Serhiy Dec 26 '16 at 08:51
  • Please check the following answer: http://stackoverflow.com/questions/17079541/angularjs-ng-repeat-with-dynamic-list-without-rebuilding-entire-dom-tree – Tome Pejoski Dec 26 '16 at 08:59