Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When an Angular 2 component has fully loaded

I cannot seem to find a hook by which I can know that a component that is made up of other child components has completely finished loading including data-binding and everything. For example take the following template for ParentComponent:

<div>
    <child-one>
        {{textOne}}
    </child-one>
    <child-two>
        {{textTwo}}
    </child-two>
</div>

Any suggestion is highly appreciated.

like image 931
lbrahim Avatar asked May 22 '16 12:05

lbrahim


People also ask

How do I reload a particular component in angular 2?

How do I reload a particular component in angular 2? In your component, import NavigationEnd: import { Router, NavigationEnd } from '@angular/router'; And then subscribe to it in your constructor: constructor(private thingService: ThisThingService, private router: Router) { router.

What is a component in angular 2?

Components are the main building block for Angular applications. Each component consists of: An HTML template that declares what renders on the page. A TypeScript class that defines behavior. A CSS selector that defines how the component is used in a template.

How many components can Angular have?

As per doc, Angular app should have mimimum one Module (root) and can have one or more components under one single module.


1 Answers

It's wrong! ngAfterViewInit is fired when the view is being initialized... Put a breakpoint into it and you will see. A hook to know when the view is actually fully loaded is really missing for Angular 2! But as a workaround you could try something like that:

ngAfterViewInit() {
    console.log('View is being initialized');
    console.log(this.el.nativeElement.offsetHeight);

    setTimeout(() => {
        console.log('View is fully loaded');
        console.log(this.el.nativeElement.offsetHeight);
    }, 0);
}
like image 67
Dimitri Avatar answered Oct 10 '22 01:10

Dimitri