Created
November 21, 2016 17:41
-
-
Save alterx/782b51b982779d8da3b6f770733b987b to your computer and use it in GitHub Desktop.
Angular 2 @input() change detection with OnChange and ngOnChange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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