簡體   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