Skip to content

Instantly share code, notes, and snippets.

@cozingo
Forked from rjmccluskey/example-component.ts
Created November 13, 2017 04:41
Show Gist options
  • Save cozingo/cfed7bc24b47656fd61e277b467b7aaf to your computer and use it in GitHub Desktop.
Save cozingo/cfed7bc24b47656fd61e277b467b7aaf to your computer and use it in GitHub Desktop.
Angular 2 Input with TypeScript property (getter and setter)
import { Component, Input } from '@angular/core';
@Component({
selector: 'example',
template: `{{data}}` // getting `data` in the template will call the getter method!
})
export class ExampleCoponent {
private dataInternal: number;
@Input() set data(data: number) {
// you might do something special in here
this.dataInternal = data;
}
get data() {
// you might do something special in here
return this.dataInternal;
}
}
<!-- `data` will be set with the setter method of ExampleComponent -->
<example [data]="123"></example>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment