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 }
    ];
  }
}

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

相关内容

热门资讯

透视数据!wejoker内置辅... 透视数据!wejoker内置辅助(透视)心悦填大坑作必弊方法(好像有辅助辅助器)-哔哩哔哩1、打开软...
今日!hhpoker德州挂真的... 今日!hhpoker德州挂真的有吗(透视)哈糖大菠萝助手(其实存在有辅助工具)-哔哩哔哩1、任何哈糖...
现场直击!wpk刷入池率脚本(... 现场直击!wpk刷入池率脚本(透视)福建天天开心辅助工具视频(都是存在有辅助辅助器)-哔哩哔哩一、福...
透视系统!aapoker怎么开... 透视系统!aapoker怎么开辅助器(透视)微乐辅助软件购买平台(总是是真的辅助修改器)-哔哩哔哩1...
推出新举措!pokemmo手机... 推出新举措!pokemmo手机版脚本(透视)中至上饶打炸辅助器开挂(一直真的有辅助app)-哔哩哔哩...
透视辅助!wepoker有没有... 透视辅助!wepoker有没有辅助(透视)葫芦娃七子连心攻略(一直真的有辅助安装)-哔哩哔哩1、下载...
于此同时!hhpoker透视脚... 于此同时!hhpoker透视脚本(透视)中至小程序破解辅助器(好像有辅助安装)-哔哩哔哩中至小程序破...
透视私人局!wpk俱乐部辅助器... 透视私人局!wpk俱乐部辅助器(透视)新上游通用卦(原来存在有辅助平台)-哔哩哔哩1、任何新上游通用...
有了最新消息!pokemmo免... 有了最新消息!pokemmo免费脚本(透视)安卓手机闲逸辅助器(其实真的有辅助平台)-哔哩哔哩1、首...
透视最新!德普之星透视辅助(透... 透视最新!德普之星透视辅助(透视)微信十三张脚本(切实真的有辅助插件)-哔哩哔哩1、完成微信十三张脚...