17.10.2018 Views

Angular

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 20: Lifecycle Hooks<br />

Section 20.1: OnChanges<br />

Fired when one or more of the component or directive properties have been changed.<br />

import { Component, OnChanges, Input } from '@angular/core';<br />

@Component({<br />

selector: 'so-onchanges-component',<br />

templateUrl: 'onchanges-component.html',<br />

styleUrls: ['onchanges-component.']<br />

})<br />

class OnChangesComponent implements OnChanges {<br />

@Input() name: string;<br />

message: string;<br />

}<br />

ngOnChanges(changes: SimpleChanges): void {<br />

console.log(changes);<br />

}<br />

On change event will log<br />

name: {<br />

currentValue: 'new name value',<br />

previousValue: 'old name value'<br />

},<br />

message: {<br />

currentValue: 'new message value',<br />

previousValue: 'old message value'<br />

}<br />

Section 20.2: OnInit<br />

Fired when component or directive properties have been initialized.<br />

(Before those of the child directives)<br />

import { Component, OnInit } from '@angular/core';<br />

@Component({<br />

selector: 'so-oninit-component',<br />

templateUrl: 'oninit-component.html',<br />

styleUrls: ['oninit-component.']<br />

})<br />

class OnInitComponent implements OnInit {<br />

}<br />

ngOnInit(): void {<br />

console.log('Component is ready !');<br />

}<br />

Section 20.3: OnDestroy<br />

Fired when the component or directive instance is destroyed.<br />

GoalKicker.com – <strong>Angular</strong> 2 Notes for Professionals 91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!