我有以下 HTML 组件:
<mat-selection-list #shoes>
<mat-list-option *ngFor="let shoe of typesOfShoes">
{{shoe.title}}
</mat-list-option>
</mat-selection-list>
<pre>
Options selected: {{this.result | json}}
</pre>
Run Code Online (Sandbox Code Playgroud)
这是 ts 组件,它包含一个数组,其中包含发送到 HTML 组件的数据。
ngOnInit() {
this.typesOfShoes = [
{
id: 1,
title: 'Aaa',
checked: true,
},
{
id: 2,
title: 'Bbb',
checked: false,
},
{
id: 3,
title: 'Ccc',
checked: true,
},
{
id: 4,
title: 'Ddd',
checked: false,
},
];
}
Run Code Online (Sandbox Code Playgroud)
我在StackBlitz 中留下了该项目的链接

如何将我在@Input() data(子组件)中收到的对象传递给dataSource,我这样做的方式不允许接收它。我在此链接中留下了我正在做的事情的示例:
@Input() data:PeriodicElement[];
dataSource = new MatTableDataSource<PeriodicElement>(this.data);
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
constructor() { }
ngOnInit() {
this.dataSource.paginator = this.paginator;
// console.log(this.dataSource)
}
Run Code Online (Sandbox Code Playgroud) 我有以下对象:
obj = [
{ 1: 20, 2: 26, 3: 14},
{ 1: 12, 2: 25, 3: 15},
{ 1: 14, 2: 13, 3: 19},
{ 1: 16, 2: 32, 3: 21}
]
Run Code Online (Sandbox Code Playgroud)
我想将每个位置乘以 2,然后将它们添加到每个位置,让我解释一下:我将每个值乘以 2,这是部分结果:
obj = [
{ 1: 40, 2: 52, 3: 28},
{ 1: 24, 2: 50, 3: 30},
{ 1: 28, 2: 26, 3: 38},
{ 1: 32, 2: 72, 3: 42}
]
Run Code Online (Sandbox Code Playgroud)
然后我必须添加每个键并通过在初始对象内的末尾添加一个新数组来添加总数,这应该是最终结果:
obj = [
{ 1: 20, 2: 26, 3: 14},
{ …Run Code Online (Sandbox Code Playgroud)