1

I got a component named "mainHomeComponent". Inside this component I got a method called "requestForPost". This method is called inside ngAfterViewInit() and when scrollbar reached the bottom of the page.

When the method requestforPost finished I store the result inside a static variabile into AppComponent.

When I move to another controller and come back to the previous one, ngAfterViewInit() method get triggered again. At this time I look for my static variabile and if this one is setted I get it's content instead of sending again a request.

But when my scrollbar reached the bottom of the page again, my json get updated but my DOM doesn't.

Code:

ngAfterViewInit()
{
    console.log("after");
    if(AppComponent.staticVar.length == 0)
    {
        this.requestForPost();  
    }
    else
    {
        this.myList = AppComponent.staticVar;
    }
}

requestForPost()
{
    ...
    sendingRequest
    ...
    (success) =>
    {
        this.myList = this.myList.concat(success);
        AppComponent.staticVar = this.myList
    })
 }

Inside requestForPost method I can see that "myList" get updated but not into DOM.

Thanks for help.

EDIT:

<eleC *ngFor="let item of myList;"></eleC >

EDIT 2 CLOSED:

At the end I managed to do it with:

this.myList = this.myList

I read this solution at: Angular2 ngFor OnPush Change Detection with Array Mutations

Marco Castano
  • 1,114
  • 1
  • 10
  • 25
  • How do you refer staticVar in your view? – Vega Jul 23 '17 at 17:33
  • I don't use staticVar. I use myList. EDIT done – Marco Castano Jul 23 '17 at 18:31
  • Don't you need '| async' ? – Vega Jul 23 '17 at 18:37
  • Is there a particular reason why you run this code inside `ngAfterViewInit` method? At this point change detection is over so I would not bet on changes in the model being reflected in the template. – Tomek Sułkowski Jul 23 '17 at 22:14
  • But inside ngAfterViewInit method when my static var is setted and I take the result from there, they are shown in the template. But when I reached the bottom with the scroll and call again requestForPost , the new posts don't appear in the template @Tomek – Marco Castano Jul 24 '17 at 05:58

0 Answers0