Home > OS >  How to call function after page fully loaded in Angular?
How to call function after page fully loaded in Angular?

Time:09-23

I am trying to invoke a function on particular route after page loaded fully, like if user logged in and come to main page route first, and it loaded successfully a function will invoke/call by itself.

constructor( private router: Router) {
  this.router.events.subscribe((event: Event) => {
   if (event instanceof NavigationEnd) { 
      
     console.log(router.url);
     //function writes here
      
    }
  });
  
  } 

I am calling this function on main.ts but when user trying to logged in this function invokes why ?

CodePudding user response:

I think if you need to use a function which calls itself or check after the page loads fully when the user logged in, users should call it in ngOnInit.

ngOnInit() {
 //please call your function
}

CodePudding user response:

This observable is called every time you change the page... try applying a filter on the page you are interested in

this.router.events
    .pipe(
      takeUntil(this.unsubscribe),
      filter((x): x is NavigationEnd => x instanceof NavigationEnd),
      map(x => x.url == 'your url')
    )
   .subscribe(...)
  • Related