如何给垫子选择选项一个动态对象键作为一个值



我想将一个动态对象传递给mat-select组件,并传递对象的输入键,该键应在[value]by mat选项中使用。此处选择

<mat-select [placeholder]="field.label" [formControlName]="field.name">
<mat-option *ngFor="let item of field.options" [value]="item">{{item}}</mat-option>
</mat-select>

代替项,它应该是动态对象键如果我像迭代一样传递这个数组

[{name:'hassa',id:1}]

对于循环,我想将id传递为在mat-option中的[value]="item"中使用但它应该是动态对象可以改变

有点晚了,但您可以使用key-value管道-类似于这个答案:https://stackoverflow.com/a/51491848/1029688

<mat-select formControlName="example">
<mat-option *ngFor="let item of testObject | keyvalue" [value]="item.key">
Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
</mat-option>
</mat-select>

您可以使用类似的

//in .ts
key="id"
text="name"
<mat-option *ngFor="let item of field.options" 
[value]="item[key]">
{{item[text]}}
</mat-option>

如果你想制作一个自定义组件来模拟复选框列表,请查看这个SO问题

最新更新