如何在Angular中预填充ngselect? [英] How to pre-populate ngselect in Angular?

查看:78
本文介绍了如何在Angular中预填充ngselect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过http调用抓取某些项目,然后想要预填充ngselect,但是使用abstractcontrol.setvalue()方法似乎无效.

I am grabbing some items through a http call and then want to pre-populate the ngselect, but using the abstractcontrol.setvalue() method does not seem to work.

模板代码

<ng-select [items]="cars"
         bindValue="code"
         bindLabel="displayName"
         formControlName="car"
         [clearable]="false"
         [searchable]="false"
         id="car"
         placeholder="Select a car">

组件代码

this.setValueForPrePopulatedPlanningDetail('car', car);

private setDefaultValue(fieldName: string, value: any) {
    if (value && value.length > 0) {
    const field = this.myFormGroup.get(fieldName);
    field.markAsDirty();
    field.setValue(value);
    }
}

推荐答案

您需要将结果设置为列表(国家/地区),然后使用setvalue选择选项

You need set your result to the list (countries) and then use setvalue to choose the option

将结果设置为组件中的列表:

Set result to your list in your component:

this.countries = ['XPTO','XPTO2','XPTO3']

在html中定义以下内容:

Define the following in your html:

<select>
   <option [value]="country" *ngFor="let country of countries">             {{country}}</option>
</select>

这篇关于如何在Angular中预填充ngselect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆