Every time I try to send data to the ts file the ngModel resets the selection in the dropdown.
<select placeholder="select" class="custom-select" id="
{{entity.parameterDisplayName}}"
[(ngModel)]="entity.parameterValue"
name="{{entity.parameterDisplayName}}">
<option [value]="-1" [selected] ="true">Select Registration Type</option>
<option *ngFor="let regTypeValue of registrationTypeList"
[ngValue]="entity.parameterValue" [value]="regTypeValue"
[selected]="regTypeValue">{{ regTypeValue }} </option></select>
I have tried using [select] and onClick as well, However as the date is being transferred successfully, And the selection from the drop down is being reset every time I click on search button below. Which will send all the information from the data filled from the form.
As modifications can be done and be searched again. The selections on the form needs to stay Put. And all the data is static, But the dropdown resets itself, Please suggest me how to stop it from getting reset.
Please help
Thanks in Advance
Your issue is that inside the select you are using
ngModel
for every option. You have to use thengValue
directive like so:here is a demo.
The ngModel registers a control with the Form element and the individual options are not separate controls because they are part of the select control.
So your code should look like (btw whenever you bind attributes to the individual elements you should use [] in favour of the interpolation syntax {{}} because it interpolates everything to a string and sometimes you would want to have a number, so my suggestion is - use the {{}} outside tags):
When using template driven forms I still haven't found a case where I would need "two-way data binding". The
ngModel
holds the value for each form control internally so in my opinion it's not good to have to manage the information in two places. You can just use[ngModel]
without the()
to bind the value to the control and then whenever you submit you can get the value from the form reference that the template variablef
holds (in the example that I showed #f="ngModel")