首页 > 解决方案 > How to know if Angular 7 component is currently loaded without using a timeout and no function on navigation

问题描述

I am trying to find a way to check if an Angular component is loaded on the page without using a timeout. Is there any way to do this? Edit: I cannot use AfterViewInit because I have an additional primeng component that it doesn't take into account.

标签: angulartypescript

解决方案


Depending on your use case a sure fire way could be to check if an element from the primeng component is visible on the page by using an interval something like this

primeNgInterval: any;
loaded = false;

// ...

checkForPrimeNgLoad() {
  this.primeNgInterval = setInterval(() => {
     let element = document.querySelector('#element');
     if (element) {
       loaded = true;
       clearInterval(this.primeNgInterval);
     }
  }, 500); 
}

推荐阅读