This is the archived documentation for Angular v6. Please visit angular.io to see documentation for the current version of Angular.

OnInit

A lifecycle hook that is called after Angular has initialized all data-bound properties of a directive. Define an ngOnInit() method to handle any additional initialization tasks.

interface OnInit { ngOnInit(): void }
      
      interface OnInit {
  ngOnInit(): void
}
    

See also

Methods

A callback method that is invoked immediately after the default change detector has checked the directive's data-bound properties for the first time, and before any of the view or content children have been checked. It is invoked only once when the directive is instantiated.

ngOnInit(): void
      
      ngOnInit(): void
    

Parameters

There are no parameters.

Returns

void

Usage notes

The following snippet shows how a component can implement this interface to define its own initialization method.

@Component({selector: 'my-cmp', template: `...`}) class MyComponent implements OnInit { ngOnInit() { // ... } }
      
      @Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnInit {
  ngOnInit() {
    // ...
  }
}