angular - 使用 ng-select 时无法读取 null 的属性 '$ngoptionlabel'

标签 angular angular-ngselect

我正在使用 ng-select显示我的应用程序从 API 检索的数据,但尽管数据已成功检索,但下拉列表中未显示任何内容。它只是说“未找到任何项目”。在控制台中,我有以下错误:ERROR TypeError: Cannot read property '$ngoptionlabel' of null这是我的 typescript 代码:

// in a service, using HttpClient
this.http.get<string[]>(url);
// in a component, using the service injected in
data: string[] = [];
this.service.get()
   .subscribe(data => {
      this.data = data; // data contains values as expected
      // other logic
   });
在我的模板中,我通过 this.datang-select .
<ng-select [items]="data"
           [multiple]="true"
           [closeOnSelect]="false"
           [searchable]="true">
</ng-select>
我认为该错误听起来可能与 bindLabel 有关。选项,但我没有使用它。错误是什么意思,我该如何解决?

最佳答案

该错误意味着正在显示的数组中的一项是 null .要解决此问题,请过滤 null在将数组传递给 ng-select 之前先将其移出数组.在从 API 检索数据的情况下,您可以使用 rxjs map运营商来实现这一点。

this.http
   .get<string[]>(url)
   .pipe(
      map(data => data.filter(x => x != null))
   );

关于angular - 使用 ng-select 时无法读取 null 的属性 '$ngoptionlabel',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62933852/

相关文章:

angular - 如何在 Angular 5 中向 ng-select 添加 "select all"功能

angular ng-select 并添加事件

<ng-select tag> 中 BindLabel 的 Angular 6 Concat 字符串

angular - 如何向 Angular 2 ng-select 添加值和标签

javascript - 如何在 ngFor 中使用引用 id ( # ) 并在下一个 NG select 中获得焦点?

jquery - 使用angular,typescript,webpack时如何将jquery添加到窗口对象

angular - p-dialog onHide 在 Angular 2 组件中不起作用 - primeng

javascript - 在JavaScript中使用push方法定义键

Angular Http 错误状态丢失

javascript - 如何将值从服务传递到组件的方法