我有一個帶有硬編碼選項的選擇,ngValue 為空,其他選項填充有 ngFor。我希望默認選擇第一個 null 選項。無論我做什么,我總是默認選擇一個空選項。
我從從 api 獲取資料的父組件獲取模型名稱和資料。
選擇組件 .html:
<select name="typeSelect" [(ngModel)]="model" (ngModelChange)="onSelectChange($event)"
aria-label="Type select">
<option [ngValue]="null">Select a type</option>
<option *ngFor="let type of object.types" [ngValue]="type">{{ type.name }}</option>
</select>
它的.ts:
@Input() object: ObjectData;
@Output() outputChosenType: EventEmitter<any> = new EventEmitter<any>();
@Input() model: string = '';
onSelectChange(chosenType: any) {
if(chosenType !== null ){...}
}
我省去了我的 ts 中的其他檢查和邏輯,因為它們作業正常,唯一不作業的是選擇了第一個選項。
該模型來自另一個帶有此組件的 ngFor。
父.html:
<app-typeSelect-card *ngFor="let object of allObjects.data ; let i = index" [object]="singleObject"
[model]="i.toString()" (outputChosenType)="onOutput($event)"></app-typeSelect-card>
uj5u.com熱心網友回復:
如果您將模型的默認值更改為 null 它將起作用。
@Input() model: string = null;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/483795.html
上一篇:在聯系表單中有多個警報框
