如何在 ng-select 上将选项设置为禁用?

How to set an option as disabled on ng-select?

使用 ng-select 版本 12。是否有将项目选项设置为禁用的选项? ...他们有一堆 samples,其中一些行被禁用,但没有解释他们是如何做到这一点的。通过检查源代码,我可以看到一些数据行的属性为 disabled:true ...我假设我所要做的就是将这个 属性 设置为我的对象,但仍然没有能够禁用一个项目。

在您的 <ng-option> 中,放置一个带有一些条件的 [disabled] 属性。

例子

<ng-option [value]="item.value" [disabled]="someFunction(item)">{{item.name}}</ng-option>

正如您所说,将disabled:true添加到要禁用的对象是正确的。

以下代码找到一个人并禁用他。

this.dataService.getPeople().subscribe((people) => {
      this.people = people;
      const p2 = this.people.find((p) => p.id === '5a15b13c728cd3f43cc0fe8a'); 
      p2.disabled = true;
    });

以下代码找到所有 isActive 属性为 false 的人并禁用他们

 this.dataService.getPeople().subscribe((people) => {
      this.people = people.map((p) =>
        p.isActive ? p : { ...p, disabled: true }
      );
    });

这是code on StackBlitz