Clearing an input text field in Angular2
Why is this method not working when I try to clear the text field?
<div>
<input type="text" placeholder="Search..." [value]="searchValue">
<button (click)="clearSearch()">Clear</button>
</div>
export class App {
searchValue:string = '';
clearSearch() {
this.searchValue = '';
}
}
What I'm expecting: since I'm passing a property for search value, when I click the button, it should get the updated value which is processed in clearSearch()
function.
I also noticed that if I set a default value to searchValue
, clearSearch()
function works, but only once.
Please check my plunker.
Solution 1:
1. First Method
you have to assign null or empty string here
this.searchValue = null;
//or
this.searchValue = ' ';
Working Plunker
because no event is being fired from angular change detection. so you have to assign some value either null or string with space
2. Second Method
- use of
[(ngModel)]
it should work withngModel
.
why ?
because as you did binding with value
attribute which is only property binding not event binding. so
angular doesn't run change detection because no event relevant to Angular is fired. If you bind to an event then Angular runs change detection and the binding works and value should be changes.
see working example of same with ngModel
Working Example with ngModel
Solution 2:
You can just change the reference of input value, as below
<div>
<input type="text" placeholder="Search..." #reference>
<button (click)="reference.value=''">Clear</button>
</div>
Solution 3:
There are two additional ways to do it apart from the two methods mentioned in @PradeepJain's answer.
I would suggest not to use this approach and to fall back to this only as a last resort if you are not using [(ngModel)]
directive and also not using data binding via [value]
. Read this for more info.
Using ElementRef
app.component.html
<div>
<input type="text" #searchInput placeholder="Search...">
<button (click)="clearSearchInput()">Clear</button>
</div>
app.component.ts
export class App {
@ViewChild('searchInput') searchInput: ElementRef;
clearSearchInput(){
this.searchInput.nativeElement.value = '';
}
}
Using FormGroup
app.component.html
<form [formGroup]="form">
<div *ngIf="first.invalid"> Name is too short. </div>
<input formControlName="first" placeholder="First name">
<input formControlName="last" placeholder="Last name">
<button type="submit">Submit</button>
</form>
<button (click)="setValue()">Set preset value</button>
<button (click)="clearInputMethod1()">Clear Input Method 1</button>
<button (click)="clearInputMethod2()">Clear Input Method 2</button>
app.component.ts
export class AppComponent {
form = new FormGroup({
first: new FormControl('Nancy', Validators.minLength(2)),
last: new FormControl('Drew'),
});
get first(): any { return this.form.get('first'); }
get last(): any { return this.form.get('last'); }
clearInputMethod1() { this.first.reset(); this.last.reset(); }
clearInputMethod2() { this.form.setValue({first: '', last: ''}); }
setValue() { this.form.setValue({first: 'Nancy', last: 'Drew'}); }
}
Try it out on stackblitz Clearing input in a FormGroup
Solution 4:
Method 1.
Using `ngModel`.
@Component({
selector: 'my-app',
template: `
<div>
<input type="text" placeholder="Search..." [(ngModel)]="searchValue">
<button (click)="clearSearch()">Clear</button>
</div>
`,
})
export class App {
searchValue:string = '';
clearSearch() {
this.searchValue = null;
}
}
Plunker code: Plunker1
Method 2.
Using null value instead of empty quotation marks.
@Component({
selector: 'my-app',
template: `
<div>
<input type="text" placeholder="Search..." [value]="searchValue">
<button (click)="clearSearch()">Clear</button>
</div>
`,
})
export class App {
searchValue:string = '';
clearSearch() {
this.searchValue = null;
}
}
Plunker code: Plunker2