【问题标题】:set selected value in angular5 ng-select programmaticaly以编程方式在角度 5 ng-select 中设置选定值
【发布时间】:2018-08-16 12:29:45
【问题描述】:

我正在使用 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】:

只要找到项目

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

然后放回去

    this.ngSelect.select(item);

您需要在 Angular 组件中引用 ng-select

    @ViewChild(NgSelectComponent)
    ngSelect: NgSelectComponent;

【讨论】:

  • ngselectcomponent 未定义
  • 以上代码对我有用。但是现在它给了我表达错误,因为我正在使用它与反应形式。角芯的 .changeDetection 方法也不起作用。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2020-03-27
  • 2018-07-25
  • 2017-06-27
  • 2019-07-08
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多