OnInit

export interface OnInit

exported from angular2/core defined in angular2/src/core/linker/interfaces.ts (line 79)

Implement this interface to execute custom initialization logic after your directive's data-bound properties have been initialized.

ngOnInit is called right after the directive's data-bound properties have been checked for the first time, and before any of its children have been checked. It is invoked only once when the directive is instantiated.

Example (live example)

@Component({ selector: 'my-cmp', template: `<p>my-component</p>` }) class MyComponent implements OnInit, OnDestroy { ngOnInit() { console.log('ngOnInit'); } ngOnDestroy() { console.log('ngOnDestroy'); } } @Component({ selector: 'app', template: ` <button (click)="hasChild = !hasChild"> {{hasChild ? 'Destroy' : 'Create'}} MyComponent </button> <my-cmp *ngIf="hasChild"></my-cmp>`, directives: [MyComponent, NgIf] }) export class App { hasChild = true; } bootstrap(App).catch(err => console.error(err));

Members

ngOnInit()

Not Yet Documented