我有3个联系人,这些联系人显示在下拉列表中,并且可选值“请选择联系人”,如下所示。
但是,页面加载时会在下拉列表中显示最后一个条目。初始加载页面时,应将可选值“请选择联系人”显示为默认初始值。
下拉列表的组件模板设置如下:
<div class="col-lg-6" *ngIf="dealer.contacts && dealer.contacts.length" >
<label>Contact person</label><br>
<select class="form-control" [ngModel]="survey.answer.contact.email" (ngModelChange)="onContactChange($event)">
<option selected [ngValue]="null">Please select contact person</option>
<option *ngFor="let contact of dealer.contacts" [ngValue]="contact.email"> {{contact.title}} {{contact.firstname}} {{contact.lastname}}, {{contact.role}}</option>
</select>
</div>
与ngModelChange相关的组件类方法如下所示。请注意,$ event值是电子邮件,当它为null时,将发回一个空对象-如果您对此有意见,也请告诉我。
onContactChange(email: any){
if (email) {
var contact: any = this.survey.answer.contact;
if (contact && contact.email == email) {
return;
}
this.survey.answer.contact = this.dealer.contacts.find((contact: any)=>{
return contact.email == email;
});
if(this.survey.answer.emailStatus != '') {
this.survey.answer.emailStatus = '';
}
this.updateSurveyContact();
} else {
this.survey.answer.contact = {};
}
}