以角度5 ng选择设置选定值 - 以编程方式选择

问题描述 投票:0回答:1

我正在使用angular5 ng-select组件:https://github.com/ng-select/ng-select并尝试在首次加载容器组件时设置所选值(以编程方式)(在模型中设置默认选择值的类型)。我没有找到任何相关属性或每个项目的isSelected。这是我的代码(已过滤):HTML:

<ng-select [items]="filter.values"  bindLabel="text" bindValue="id" class="input-md" [(ngModel)]="filter.selectedValue"></ng-select>

模型:

export class FilterData
{
    Name : string;
    FormattedName : string;
    values : Filter[];
    selectedValue : Filter = null;
    constructor(filterData : FilterData)
    {
        this.Name = filterData.Name;
        this.values = filterData.values;
        this.selectedValue = typeof filterData.selectedValue == "undefined" ? null : filterData.selectedValue;
    }
}

export class Filter 
{
    public id: number;
    public text: string;
}
angular5 selecteditem selectedvalue angular-ngselect
1个回答
4
投票

只需找到该项目

let item = this.ngSelect.itemsList.findByLabel('label of the item');

然后将其设置回来

this.ngSelect.select(item);

你需要在角度分量中引用ng-select

@ViewChild(NgSelectComponent)
ngSelect: NgSelectComponent;
© www.soinside.com 2019 - 2024. All rights reserved.