I have an array of tranTypes
(transaction types) that are loaded into a dropdown. After the user selects a value and navigates to another component upon returning the value is not selected in the dropdown.
From other readings, I've learned this is b/c the objects are not the same instance. What do I do in this situation then?
<select name="tranType"
class="form-control"
[(ngModel)]="model.tranType"
required>
<option *ngFor="let tranType of tranTypes"
[ngValue]="tranType">{{tranType.desc}}</option>
</select>
Solution
ngOnInit(): void {
this.myService.getTranTypes()
.subscribe(tranTypes => {
this.tranTypes = tranTypes;
//set value of tranType if already set in the model
if (this.myService.model.tranType != undefined) {
this.myService.model.tranType = this.tranTypes.find(r => r.id == this.myService.model.tranType.id);
}
},
error => this.errorMessage = <any>error);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…