How to improve performance in Angular 2 with a multi-dimensional List with actions?

23 views
Skip to first unread message

Jean Baptiste MARCHETTI

unread,
Dec 8, 2016, 5:10:00 AM12/8/16
to Angular

Hello i am developing an application with Angular2. I recieve via an api a multi-dimensional list that I must display in an html table.

Here is the simplified version of the json that I receive from the api:


 {
  "events": [
    {
      "id": 0,
      "name": 0,
      "sub_events": [
        {
          "id": 0,
          "name": 0,
          "actions": [
            {
              "id": 0,
              "name": 0
            }
          ]
        }
      ]
    }
  ]
}


I tried to isolate the components when displaying like this:


ListComponent:


<event-view  [event]='event' *ngFor="let event of events"></event-view>


EventComponent: (event-view selector)


<div>
<h1>{{event.id}} {{event.name}}</h1>
<table>
  <tr subevent-view [subEvent]='subEvent' *ngFor="let subEvent of event.sub_events"></tr>
</table>
</div>


SubEventComponent: (subevent-view selector)


<td>{{subEvent.id}}</td>
<td>{{subEvent.name}}</td>
<td>
  <event-action  [action]='action' *ngFor="let action of subEvent.actions"></event-action>
</td>


EventActionComponent: (event-action selector)


<button (click)='....' >{{action.name}}</button>


All my components use changeDetection: ChangeDetectionStrategy.OnPush

I would like that when I click on an action Angular 2 does not recheck the whole list but only the clicked button.

For now Angular does a check on the list + one check on all events + one on all subevents + one on all actions ...

I tried using ChangeDetectorRef.detach but it does not change anything.

My root list events is created with ImmutableJS.

Is there a way to tell angular to check only one child component without re-checking the whole tree?


 I ve made a plunker of my problem http://plnkr.co/edit/aVMD4IMg6bxwcMvhZ271?p=preview
(the checks are logged in the console)


If someone has already had problems with unnecessary checking
Help me please :wink:

Thank you in advance ;)



Sander Elias

unread,
Dec 9, 2016, 12:55:34 AM12/9/16
to Angular
Hi Jean,

Actually, your sample does exactly what you want. However, if you use the ngDoCheck you are forcing a component to react on change detection! (actually, it does nothing aside from logging in your sample!) To get a feel on when actual change detaction is fired, you need to use ngOnChanges wich is only fired when changes for that component are detected! I forked your plunk with this change

Hope this clarfies it enough for you.
Regards
Sander
Reply all
Reply to author
Forward
0 new messages