ngOnInit does not get invoked when routing to same component again - Angular 2

十年热恋 提交于 2019-12-14 03:08:08

问题


I'm working on angular 2 webapp, I navigate to xyz component when I have data in my array and stores it in service component. The xyz component's ngOnInit() method get data from service and displays it to its html template. The problem is that when my source array data changes I'm redirecting to xyz component again but then xyz's ngOnInit() method does not get invoked this time. Is there any way to achieve this ?


回答1:


You could subscribe to an event in your component. I don't know how to integrate this to you code, but on mine it looks like this :

ngOnInit() {
    this._events.xyz.onDataChange.subscribe (() => {
        this.onDataChange();
    }
onDataChange() {
    // Display to HTML Template
}

And then, when your data changes, you use this._events.xyz.onDataChange().publish()

I'm using a TypeScript class EventsManager, you can probably do this kind of things with angular's events.

You should wait for answers from more experienced developers though.



来源:https://stackoverflow.com/questions/43652845/ngoninit-does-not-get-invoked-when-routing-to-same-component-again-angular-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!