Skip to content

Instantly share code, notes, and snippets.

@alterx
Last active November 22, 2016 17:32
Show Gist options
  • Save alterx/4d7fe100a7df688618ec6febf46b7a2e to your computer and use it in GitHub Desktop.
Save alterx/4d7fe100a7df688618ec6febf46b7a2e to your computer and use it in GitHub Desktop.
Angular 2 @input() change detection with TypeScript accessors
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