Why is this method not working when I try to clear the text field?
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>
@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
@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
HTML
<input type="text" [(ngModel)]="obj.mobile" name="mobile" id="mobile" class="form-control" placeholder="Mobile/E-mail" />
TS
onClickClear(){
this.obj.mobile= undefined;
}
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.
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 = '';
}
}
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
Template driven method
#receiverInput="ngModel" (blur)="receiverInput.control.setValue('')"
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
[(ngModel)]
it should work with ngModel
.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