首页 > 解决方案 > How is browser's debugger different from Angular's setTimeout()?

问题描述

I am working on an angular app which is embedded in an application I don't have access to. I am using a router to move from page to page once a pop-up modal has been OK'd (checkbox & button). I have to ensure that the modal has been destroyed before moving to the next page or the outer page causes problems. I have tried using *ngif="destroyModal". It works, but it's not happening in-time.

When I set the browser's debugger to stop the action on line with this.myRequestService.update... it seems to stop the action in such a way that my modal destroy works. If I use a debugger break-point on the next line (in the called-method) it doesn't work.

public goToPageTwo(): void {
    this.myObject = {};

    this.myRequestService.updateNextPg(this.myObject).subscribe((nextPageResponse) => {
        this.nextPageService.setData(nextPageResponse);
        this.router.navigateByUrl(NEXT_PAGE);
    }
}

I have tried several approaches with setTimeout(() => this.router.navigateByUrl(NEXT_PAGE), 2000) but this doesn't seem to have any effect. The only stop that allows the modal destroy to happen is one delivered by a debugger break-point.

I can't ask the user to do this :(

标签: javascriptnode.jsangulardom-events

解决方案


您应该在ngOnDestroy()生命周期事件中使用页面导航。

myClass {
  ...
  ngOnDestroy(){
    //do it here
  }
}

当组件关闭和清理时,这将是您想要引起页面导航的地方。


推荐阅读