繁体   English   中英

Ng-Select Selected 未从 ngModel 更新

[英]Ng-Select Selected are not updating from ngModel

我遇到了一个让我难倒的问题。 我有一个 ng-select。 将记录加载到 ngModel 时,它不会更改 ng-select。 最奇怪的部分是在另一个页面上,相同的代码有效。

我尝试了许多不同的选项,甚至创建新数组只是为了测试。 还尝试了 setTimeout 以查看是否有什么东西正在清除选择。

HTML代码

  <label>
    Add a Secondary channel
  </label>
  <br />
  <ng-select placeholder="Select schools" items="schoolsArray" [(ngModel)]="selectedSecondSchools" [multiple]="true" name="secondSchools"
  #secondSchools="ngModel">
  </ng-select>
</div>

组件中的一个函数。 该数组声明为 selectedSecondSchools[] = [];

getVodEventGameSecondaryChannels(vodEventId: number, vodEventGameId: number): string[] {
    let schoolIds: string[] = [];

    this.vodEventService
      .getVodEventGameSecondaryChannelsByGameId(vodEventId, vodEventGameId)
      .subscribe((sc: IVodEventGameSecondaryChannels[]) => {
        sc.forEach(gsc => {
          schoolIds.push(
            gsc.schoolId.toString());
        });
      });

    this.eventGame.gameSecondaryChannelIds = schoolIds;
    this.selectedSecondSchools = schoolIds;

    return schoolIds;
  }

返回数据时,需要在ng-select中选择返回的ID并显示。

这取决于schoolsArray的类型。 在你的情况,我想, schoolsArray是对象的列表,而ngModel是一个字符串数组。 NG-选择不知道如何从字符串映射ngModel从对象items

请参阅属性bindValue文档以通知 ng-select 如何将items键绑定到模型。 https://github.com/ng-select/ng-select

总而言之,将bindValue="id-property-name-in-items"到 ng-select 标签。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM