2

我正在使用 Ng-Zorro 的多项选择,它位于抽屉中。打开抽屉时,我给选择元素一个选项列表和一个已经选择的项目列表。可供选择的选项列表工作正常,但已选择的项目不显示。这也可以在这里看到: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 显示选择了一个额外的项目。

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

4

1 回答 1

3

尝试如下。

selectedAttributes = [this.allAttributes[0]];

自从

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

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

于 2019-11-29T20:37:34.500 回答