Last active
November 22, 2016 17:32
-
-
Save alterx/4d7fe100a7df688618ec6febf46b7a2e to your computer and use it in GitHub Desktop.
Angular 2 @input() change detection with TypeScript accessors
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 } from '@angular/core'; | |
@Component({ | |
selector: 'my-name', | |
template: ` | |
<h2>First name: {{name}} ({{_name}})</h2> | |
`, | |
}) | |
export class ChildComponent implements OnInit { | |
private _name: string; | |
constructor() {} | |
get name(): string { | |
// transform value for display | |
return this._name.toUpperCase(); | |
} | |
@Input() | |
set name(name: string) { | |
console.log('prev value: ', this._name); | |
console.log('got name: ', name); | |
this._name = name; | |
} | |
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