簡體   English   中英

如何設置在 ng-select angular 8 上選擇的第一個選項?

[英]How can I set first option selected on ng-select angular 8?

我正在從嵌套數組生成多個下拉列表。 我想為所有下拉菜單選擇第一個選項。 這是我在 html 上的代碼

<ng-container *ngFor="let siz of product.attributes">
     <ng-select autocomplete="attributes" formControlName="attributes" [ngClass]="{ 'is-invalid': submitted && val.get('attributes').errors }" class="mt-2" bindValue="{{ siz }}" bindLabel="{{ siz }}" [items]="siz.value" placeholder="Select {{ siz.name }}" (change)="sizeChange($event, siz.name)"></ng-select>
</ng-container>

其他一切正常,我只需要默認選擇第一個選項。

這是屬性數組在此處輸入圖像描述

在 onInit 中獲取所有內容之前的值

selected = product.attributes.map(x => x.value.map(y => y)[0])[0];

在初始化表單時使用方法 1

方法一:

//fb is formbuilder
this.form = fb.group({
   attributes: [selected, Vaidators.required] // selected is the default value
});

方法二:表單初始化后

this.form.controls['attributes'].setValue(selected);

.ts 文件

<ng-select formControlName="attributes">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM