gpt4 book ai didi

html - Ng-Zorro 选择不显示带有 NgModel 的选定项目

转载 作者:行者123 更新时间:2023-12-05 02:58:23 35 4
gpt4 key购买 nike

我正在使用 Ng-Zorro 的多重选择,它在抽屉里。打开抽屉时,我给 select 元素一个选项列表和一个已经选择的项目列表。可供选择的选项列表工作正常,但已选择的项目不显示。这也可以在这里看到:StackBlitz

代码:

<nz-select [(ngModel)]="selectedAttributes"
[nzMaxTagCount]="3"
[nzMaxTagPlaceholder]="attributeTagPlaceHolder"
nzMode="multiple"
name="changeAttributes"
id ="changeAttributes"
nzPlaceHolder="Please select">
<nz-option *ngFor="let attribute of allAttributes" [nzLabel]="attribute.name" [nzValue]="attribute"></nz-option>
</nz-select>
<ng-template #attributeTagPlaceHolder let-selectedList> "and " {{ selectedList.length }} " more items" </ng-template>

其中 allAttributes 列表的格式如下:

allAttributes= [
{
"id": 1,
"name": "Mask"
},
{
"id": 2,
"name": "Intensive"
},
{
"id": 3,
"name": "Family"
},
{
"id": 4,
"name": "Isolation"
}

];

并且 selectedAttributes 是 allAttributes 列表中的一项或多项:

selectedAttributes= [{"id": 1,"name": "Mask"}];

无论我如何创建或格式化所选属性列表(它可以直接来自 allAttributes 列表),都看不到占位符并且选择为空,加上选择所有选项时,nzMaxTagPlaceholder 显示有一个额外的项目采摘。

谁能告诉我动态设置选中项的方法?

最佳答案

像下面这样尝试。

selectedAttributes = [this.allAttributes[0]];

{"id": 1,"name": "Hapnikumask"}

是一个复杂的对象,它的相等性将通过引用来检查。因此,您正在定义一个新对象作为选定对象,它将与源对象不同。

关于html - Ng-Zorro 选择不显示带有 NgModel 的选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59110624/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com