Angular 2 do not refresh view after array push in ngOnInit promise

后端 未结 2 1845

I created a NativeScript app with angular 2, i have an array of objects that i expect to see in the frontend of the application. the behaviour is that if i push an object in

2条回答
  •  旧时难觅i
    2021-01-17 12:07

            setTimeout(function () {
                this.stories.push(story);
            }, 0);
    

    I had reall trouble with pushing into nested array, with almost random refresh results, till i stumped on this spec :

    Basically application state change can be caused by three things:

    • Events - click, submit, …

    • XHR - Fetching data from a remote server

    • Timers - setTimeout(), setInterval()

    (https://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html#what-causes-change)

    So I tried setTimeout, and miraculously it worked ...

提交回复
热议问题