【问题标题】:Angular 7 - Passing data to child component properlyAngular 7 - 正确地将数据传递给子组件
【发布时间】:2019-07-03 20:07:11
【问题描述】:

我正在尝试使用 for 循环生成 3 个 Google 图表。每个 Google 图表都应在饼图中显示不同的数据。

为此,我将 Google Chart 嵌入到一个子组件中,该子组件从父组件获取一些输入变量并呈现图表。

我注意到所有 3 个图表都只显示最后传递的数据,而不是为每组数据显示不同的图表。我在这里做错了什么?

Stackblitz

app.component.html

<div *ngFor="let result of results; index as i;">
    <app-google-chart #chart{{i}} 
    [title]="title"
    [type]="type"
    [data]="data"
    [columnNames]="columnNames"
    [options]="options"
    [width]="width"
    [height]="height">

    </app-google-chart>
</div>

app.component.ts

export class AppComponent implements OnInit {

  constructor() {}

  results = ["1","2","3"];

  title;
  type;
  data;
  columnNames;
  options;
  width;
  height;

  ngOnInit() {

    for(let i=0;i<this.results.length;i++)
    {
      this.drawChart(i);
    }
  }

  drawChart(index:any) {
    var passed;
    var failed;

   if(index == 0) 
    {
      passed=50;
      failed=50;
    }
    else if(index == 1) 
    {
      passed=20;
      failed=30;
    }
    else if(index == 2) 
    {
      passed=40;
      failed=60;
    }
    this.title = "Chart" + index;
    this.type = "PieChart";
    this.data = [['Pass', passed], ['Fail', failed]];

  }
}

【问题讨论】:

  • [data]="data":您将相同的AppComponent.data 传递给每个图表。您的 TypeScript 代码没有多大意义:您在循环的每次迭代中都覆盖了相同的实例变量。如果你想要组件中的三个不同的数据,你需要一个由 3 个元素组成的数组。
  • hmm.. 我不确定如何修复我的打字稿代码以将唯一值传递给子组件。

标签: angular


【解决方案1】:

根据您的app.component.ts,您正在使用不同的值调用相同的函数drawChart(index) 来替换相同的值。

所以唯一的最后一次迭代存在。 看一下这个 STACKBLITZ

希望这会有所帮助:)

【讨论】:

  • 忘记分叉你的堆栈闪电战,所以变化没有反映。现在检查链接。
【解决方案2】:

您可以将属性推送到 results 数组并在 html 中使用它的属性,而不是更新相同的变量

<div *ngFor="let result of results; index as i;">
    <app-google-chart #chart{{i}} 
    [title]="result.title"
    [type]="result.type"
    [data]="result.data"
    [columnNames]="columnNames"
    [options]="options"
    [width]="width"
    [height]="height">

    </app-google-chart>
</div>
export class AppComponent implements OnInit {

  // or a different set of data to be looped through in OnInit
  dataLength = 3;

  // data to be passed to children component
  results = [];

  ngOnInit() {
    for(let i = 0;i < dataLength; i++) {
      this.results.push(this.drawChart(i));
    }
  }

  drawChart(index:any) {
    var passed;
    var failed;

   if(index == 0) {
      passed=50;
      failed=50;
    } else if(index == 1) {
      passed=20;
      failed=30;
    } else if(index == 2) {
      passed=40;
      failed=60;
    }

    // instead of this
    this.title = "Chart" + index;
    this.type = "PieChart";
    this.data = [['Pass', passed], ['Fail', failed]];

    return { 
       title: 'Chart' + index,
       type: 'PieChart',
       data: [['Pass', passed], ['Fail', failed]]
    };
  }
}

【讨论】:

  • 这行不通。 ngOnInit 中的结果大小为 0。结果将保持为空。
  • @KautilyaKatiha 你是对的,我完全错过了。我更新了我的答案
猜你喜欢
  • 2019-06-10
  • 1970-01-01
  • 2020-05-26
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
相关资源
最近更新 更多