值必须是角度材料垫选择中处于多选模式的数组



我正在使用角度材料垫选择组件。

<mat-table [dataSource]="dataSource" class="mat-elevation-z8" matSort >
<ng-container matColumnDef="sensitive">
<mat-header-cell class="table-header header-p" *matHeaderCellDef> <b>sensitive</b> </mat-header-cell>
<mat-cell class="table-content context-position "  *matCellDef="let element"  >
<mat-select placeholder="sensitive"  multiple [(ngModel)]="element.sensitive" >
<mat-option *ngFor="let type of sensitiveList" [value]="type">{{type}}</mat-option>
</mat-select>
</mat-cell>
</ng-container>
<mat-header-row class="table-header-row" *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row  [ngClass]="((i%2) == 1) ? 'table-content-row-single' : 'table-content-row-double'"  *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

打字稿 敏感列表 : 字符串[] = [ 'none', 'sensitive ']; for(var i=0;我

为什么运行代码会给我一个错误

Value must be an array in multiple-selection mode

你有一个多选,它正在提供数据数组,而你的元素属性"敏感"不是一个数组!将您的属性"敏感"更改为字符串数组或从 mat-select 中删除"倍数"以获得单个值,您的问题应该得到解决

我遇到了这个问题。我作为数组传递,但仍然收到错误。对我来说,问题是我为表单Contol设置了一个默认值。 例如:

myFormControl: new FormControl(1);

问题是默认值应该是一个数组。 因此,若要为具有multiple属性的mat-select设置默认值,应执行以下操作:

myFormControl: new FormControl([1]);

完成此操作后,问题就解决了。

最新更新