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

5
  • How do you refer staticVar in your view? Commented Jul 23, 2017 at 17:33
  • I don't use staticVar. I use myList. EDIT done Commented Jul 23, 2017 at 18:31
  • Don't you need '| async' ? Commented Jul 23, 2017 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. Commented Jul 23, 2017 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 Commented Jul 24, 2017 at 5:58

0

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.