Reputation: 15
In example below the input is hidden at first, then user pushes the button and the button is visible. Should variable showInput be declared in components typescript file? From the functionality point of view I know it doesn't need to be, because the example below works regardles if showInput is property of component or not.
@Component({
selector: 'example',
template: '
<button (click)="showInput = !showInput">Show input</button>
<input *ngIf="showInput" type="text">
'
})
export class Example{}
Upvotes: 0
Views: 56
Reputation: 791
I would declare it in the component ts. If in the future, a programmer will need to append the functionality by either executing something before or after the opening of the button, or creates a condition which decides whether or not the button should be shown, then he will have to first find that template variable and then rewrite a bunch of code. It's a lot better to just do this in your component.ts file
Upvotes: 0
Reputation: 18399
You should declare them, if you plan to use Ahead of Time compilation (which you should for production build, as it will generate much smaller bundles).
When using AoT, your example will not compile with error
Property 'showInput' does not exist on type 'Example'.
Upvotes: 2