In the following example i want to be able to reset input field to default value, (after user might have changed the input value manually)
the main problem is, that default value never changes, so re-setting it does nothing
Template
JavaScript
x
6
1
<div>
2
<label>Select a number </label>
3
<input [value]="defaultValue" />
4
<button (click)="onClick()">Reset to default Number</button>
5
</div>
6
Component
JavaScript
1
15
15
1
import { Component } from '@angular/core';
2
3
@Component({
4
selector: 'my-app',
5
templateUrl: './app.component.html',
6
styleUrls: ['./app.component.css'],
7
})
8
export class AppComponent {
9
defaultValue = 10;
10
onClick() {
11
//wont work, because we are not changing the default.
12
this.defaultValue = 10;
13
}
14
}
15
Advertisement
Answer
You could use Template variable:
JavaScript
1
3
1
<input [value]="defaultValue" #input />
2
<button (click)="input.value = 10">Reset to default Number</button>
3
See StackBlitz example.