-
-
Save cozingo/cfed7bc24b47656fd61e277b467b7aaf to your computer and use it in GitHub Desktop.
Angular 2 Input with TypeScript property (getter and setter)
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 } 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; | |
} | |
} |
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
<!-- `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