Skip to content

Instantly share code, notes, and snippets.

@rjmccluskey
Last active October 17, 2021 04:47
Show Gist options
  • Save rjmccluskey/46dfe9ed6abd4990b4f1a03263ace6de to your computer and use it in GitHub Desktop.
Save rjmccluskey/46dfe9ed6abd4990b4f1a03263ace6de 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