Skip to content

Instantly share code, notes, and snippets.

@alterx
Created November 21, 2016 17:41
Show Gist options
  • Save alterx/782b51b982779d8da3b6f770733b987b to your computer and use it in GitHub Desktop.
Save alterx/782b51b982779d8da3b6f770733b987b to your computer and use it in GitHub Desktop.
Angular 2 @input() change detection with OnChange and ngOnChange
import {
Component, Input,
OnInit,
OnChanges, SimpleChanges, SimpleChange
} from '@angular/core';
@Component({
selector: 'my-last-name',
template: `
<h2>Last name: {{_name}} ({{ name }})</h2>
`,
})
export class OtherChildComponent implements OnChanges, OnInit {
@Input() name: string;
private _name: string;
constructor() {}
ngOnChanges(changes: SimpleChanges) {
const name: SimpleChange = changes.name;
console.log('prev value: ', name.previousValue);
console.log('got name: ', name.currentValue);
this._name = name.currentValue.toUpperCase();
}
ngOnInit() {
console.log('on init');
console.log(this.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment