这是我的代码:
<select label="people" id="ppl"  [(ngModel)]="Selectedppl" (ngModelChange)="onPplSelection($event.target.value)">
<option>select people</option>
<option *ngFor="let x of peopleList" [ngValue]="x">
    {{x.name}}
</option>
因为人员列表是一个具有名称、地址、联系人的对象。我想以params的形式向ngModelChange和ngValue发送对象。
但是,一旦我选择了一个特定的人并保存下来,我希望将下拉列表更改为选定的人的名字。基本上,我想显示默认的选择选项在下拉列表中,一旦保存。
我不使用Reactiveforms
发布于 2018-11-11 10:39:46
您可以拥有元素的引用并将其值传递给函数。
<select label="people" id="ppl" #ppl="ngModel"  
   [(ngModel)]="Selectedppl" 
   [compareWith]="compareFn"
   (ngModelChange)="onPplSelection(ppl.value)">
     <option>select people</option>
     <option *ngFor="let x of peopleList" [ngValue]="x">
       {{x.name}}
     </option>
</select>ts
如果您有复杂的对象,那么您必须使用ue compareFn,这有助于比较对象的角度来设置默认值。
  compareFn(a, b) {
    if (!a || !b) {
      return false;
    } else {
      return a.name === b.name;
    }
  }https://stackoverflow.com/questions/53247745
复制相似问题