简体   繁体   English

Ng-Select Selected 未从 ngModel 更新

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

I'm experiencing a problem that has me stumped.我遇到了一个让我难倒的问题。 I have an ng-select.我有一个 ng-select。 When loading records to the ngModel, it doesn't change the ng-select.将记录加载到 ngModel 时,它不会更改 ng-select。 The weirdest part is that on another page, the same code works.最奇怪的部分是在另一个页面上,相同的代码有效。

I've tried numerous different options, even creating new arrays just to test.我尝试了许多不同的选项,甚至创建新数组只是为了测试。 Also tried setTimeout to see if something is clearing the selection.还尝试了 setTimeout 以查看是否有什么东西正在清除选择。

The HTML code 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>

The one function in the component.组件中的一个函数。 The array is declared as selectedSecondSchools[] = [];该数组声明为 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;
  }

When the data is returned, the returned IDs need to be selected within the ng-select and displayed.返回数据时,需要在ng-select中选择返回的ID并显示。

It depends on the type of schoolsArray .这取决于schoolsArray的类型。 In your case, I guess, schoolsArray is a list of objects, while ngModel is an array of strings.在你的情况,我想, schoolsArray是对象的列表,而ngModel是一个字符串数组。 ng-select doesn't know how to map strings from ngModel to objects from items . NG-选择不知道如何从字符串映射ngModel从对象items

Please see documentation for property bindValue to inform ng-select how to bind items keys to the model.请参阅属性bindValue文档以通知 ng-select 如何将items键绑定到模型。 https://github.com/ng-select/ng-select https://github.com/ng-select/ng-select

To summarize, add bindValue="id-property-name-in-items" to ng-select tag.总而言之,将bindValue="id-property-name-in-items"到 ng-select 标签。

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

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