Angular中从子组件循环表格数据无法正确显示。
创始人
2024-10-31 01:30:33
0

这个问题的原因在于在子组件中循环的数据无法正确传递到父组件中的表格中。为了解决这个问题,需要增加一些额外的代码来确保数据正确传递和显示。

  1. 在父组件中添加一个属性来保存子组件中需要循环的数据:
// parent.component.ts
export class ParentComponent {
  childData: any[] = [];
}
  1. 在子组件中,将需要循环的数据通过@Input()装饰器传递到父组件中:
// child.component.ts
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    
          ...
        
{{ item.name }} {{ item.age }}
` }) export class ChildComponent { @Input() data: any[] = []; }
  1. 在父组件的模板中,将子组件的数据通过@Input()装饰器传递给子组件:
// parent.component.html

  1. 在父组件中,将需要循环的数据赋值给新增的属性:
// parent.component.ts
export class ParentComponent implements OnInit {
  childData: any[] = [];

  ngOnInit(): void {
    this.childData = [
      { name: 'Alice', age: 25 },
      { name: 'Bob', age: 30 },
      { name: 'Charlie', age: 35 }
    ];
  }
}

这样,子组件中的数据就能正确地循环并显示在父组件的表格中了。

相关内容

热门资讯

五分钟法子!hhpoker是真... 五分钟法子!hhpoker是真的还是假的(透视)总是有辅助辅助(哔哩哔哩)1、超多福利:超高返利,海...
第5分钟手筋!wpk俱乐部是做... 第5分钟手筋!wpk俱乐部是做什么的(透视)都是有辅助工具(哔哩哔哩)一、wpk俱乐部是做什么的可以...
第六分钟妙招!pokemmo脚... 第六分钟妙招!pokemmo脚本手机版(透视)真是是有辅助开挂(哔哩哔哩)1、每一步都需要思考,不同...
8分钟练习!wepoker怎么... 8分钟练习!wepoker怎么提高运气(透视)一直真的是有辅助下载(哔哩哔哩)1、首先打开wepok...
第4分钟手册!wpk作必弊(透... 第4分钟手册!wpk作必弊(透视)原来真的有辅助神器(哔哩哔哩)第4分钟手册!wpk作必弊(透视)原...
第三分钟演示!wepoker正... 第三分钟演示!wepoker正确养号方法(透视)一直真的是有辅助安装(哔哩哔哩)1、让任何用户在无需...
1分钟手段!hhpoker免费... 1分钟手段!hhpoker免费透视脚本(透视)好像存在有辅助工具(哔哩哔哩)1、任何hhpoker免...
第3分钟模板!wejoker辅... 第3分钟模板!wejoker辅助器怎么卖(透视)原来存在有辅助教程(哔哩哔哩)运wejoker辅助器...
一分钟大纲!wepoker私人... 一分钟大纲!wepoker私人局辅助(透视)总是存在有辅助工具(哔哩哔哩)1、金币登录送、破产送、升...
第八分钟窍要!德普之星有辅助软... 第八分钟窍要!德普之星有辅助软件吗(透视)切实有辅助透视(哔哩哔哩)该软件可以轻松地帮助玩家将德普之...