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

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

相关内容

热门资讯

透视解迷"pokem... 透视解迷"pokemmo辅助官网"本来有辅助攻略(哔哩哔哩)1、每一步都需要思考,不同水平的挑战会更...
透视揭露"wepok... 透视揭露"wepoker可以开透视吗"一直存在有辅助神器(哔哩哔哩)1、该软件可以轻松地帮助玩家将w...
透视曝光"wepok... 透视曝光"wepoker游戏的安装教程"切实是真的辅助教程(哔哩哔哩)亲,关键说明,wepoker游...
透视科普"wepok... 透视科普"wepoker作必弊辅助"其实是有辅助教程(哔哩哔哩)1、任何wepoker作必弊辅助透视...
透视有挂"wejok... 透视有挂"wejoker黑侠辅助器"果然真的是有辅助攻略(哔哩哔哩)1、让任何用户在无需wejoke...
透视必备"pokem... 您好,pokemmo修改器手机版这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...
透视专业"wepok... 透视专业"wepoker正确养号方法"果然真的有辅助神器(哔哩哔哩)亲,关键说明,wepoker正确...
透视辅助"wpk软件... 透视辅助"wpk软件是正规的吗"一贯是真的辅助软件(哔哩哔哩)1、wpk软件是正规的吗透视辅助软件激...
透视辅助"德州私人局... 透视辅助"德州私人局脚本"都是真的是有辅助神器(哔哩哔哩)1、该软件可以轻松地帮助玩家将德州私人局脚...
透视教你"xpoke... 透视教你"xpoker辅助控制"一贯是真的辅助挂(哔哩哔哩)1.xpoker辅助控制 选牌创建新账号...