Reputation: 2044
I have drop down and text field. The selected value from drop down displayed in text field. Now I want to clear the selected value i.e; display empty on drop down but for the first time it clearing and for rest it not working.
I just want to show empty space on dropdown for every selection
Upvotes: 2
Views: 3444
Reputation: 73731
You can make your code work by forcing change detection before resetting the bound value. This probably ensures that the change made in the select
element is processed before we then set it to an empty value. See this stackblitz.
logNoteSelectionChange(obj) {
this.Otherlogs = obj;
this.changeDetectorRef.detectChanges();
this.selectedLog = '';
}
Alternatively, you could remove the [(ngModel)]
data binding on the select
element:
<select #select ngModel (ngModelChange)="logNoteSelectionChange(select, $event)">
<option *ngFor="let log of logCol">
{{log.logMessage}}
</option>
</select>
and reset the selected value in the (ngModelChange)
event handler:
logNoteSelectionChange(select: HTMLSelectElement, obj: string) {
this.Otherlogs = obj;
select.value = "";
}
See this stackblitz for a demo.
Upvotes: 1
Reputation: 1955
If i understood you correct and rely on previous question it is more clearer resolution
`<select #select class="form-group"
(change)="logNoteSelectionChange(select,select.value)">
<option *ngFor="let log of logCol">
{{log.logMessage}}
</option>
</select>
`
here is a link to stackblitz
Upvotes: 0