I'm trying to set an initial value when filling a select, the data is coming from rest and loaded in the component. The initial value is not set/updated after the
Getting the list to fill the select:
...
ngOnInit():any {
this.crudService.get(`/api/countries`).subscribe(data => {this.countries = data;});
...
Getting the model object witch has a Country:
...
this.crudService.get('/api/unions/default').subscribe(data => {
this.union = data;
...
HTML:
...
<div class="col-md-2">
<select *ngIf="union.country" [(ngModel)]="union.country" class="form-control">
<option *ngFor="let c of countries" [ngValue]="c">{{c.code}}</option>
</select>
</div>
...
Everything works, I can select a country and it is correctly updated in the model but the initial value is not selected but existing in the model... Any suggestions or is this a bug?
Thanks!
If the default value is assigned to union.country
and it is an object (vs a primitive value), it needs to be the same instance, not only an object with the same content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With