javascript - Angular 子组件无法识别输入更改

你好我用的是亲子交流。在 parent 那里,我有一个代表图形数据的数组。 Demo

我的问题是,当我更改数组中的一项时,我不会触发子组件应用程序更改。在演示中,我创建了不起作用的示例。当我点击更新时,我想更新子组件图。

我的子组件

import { Component, OnInit, Input, OnChanges } from "@angular/core";
import * as Highcharts from "highcharts";
import { ChartService } from "./chart.service";

@Component({
  selector: "hello",
  template: `
    <highcharts-chart
      [Highcharts]="Highcharts"
      [options]="options"
      [oneToOne]="true"
      [update]="updateFromInput"
      style="width: calc(100% ); height: calc(100% - 17px); display: block;margin-top:15px;overflow: auto !important;"
    >
    </highcharts-chart>
  `,
  styles: [
    `
      h1 {
        font-family: Lato;
      }
    `
  ]
})
export class HelloComponent implements OnInit, OnChanges {
  innerHeight: any;
  innerWidth: any;

  @Input() data: any;

  Highcharts: typeof Highcharts = Highcharts;
  updateFromInput = false;
  options: any;

  constructor(private chart_service: ChartService) {
    this.innerHeight = window.screen.height;
    this.innerWidth = window.screen.width;
  }

  onResize(event) {
    this.innerWidth = event.target.innerWidth;
    this.update();
  }

  ngOnInit() {
    this.update();
  }

  ngOnChanges() {
    console.log(this.data);
    console.log("değişti");
    this.update();
  }

  update() {
    var series = this.chart_service.groupBy(
      this.data.LINE.DATA,
      "GRUP",
      "line"
    );
    var categories = this.chart_service.ArrNoDupe(
      this.data.LINE.DATA.map(x => x.NAME)
    );
    let isLegend = series.length > 1 ? true : false;
    var size = this.innerWidth / (12 / this.data.SIZE) - 30;
    if (this.innerWidth <= 992) {
      size = this.innerWidth - 30;
    }
    this.options = {
      title: { enabled: false, text: "" },
      credits: { enabled: false },
      legend: { enabled: true },
      tooltip: { hideDelay: 0, outside: true, shared: true },
      plotOptions: {
        line: { dataLabels: { enabled: !isLegend }, enableMouseTracking: true }
      },
      yAxis: { title: { enabled: false } },
      xAxis: { categories: categories },
      series: series
    };
  }
}

最佳答案

在你的 app.component.ts 中:

filter(id) {
    this.reports.filter(x => x.ID == id)[0] = {
      ID: 3233.0,
    ...
   }
}

Array.prototype.filter 不会改变给定的数组,而是返回一个具有过滤属性的新数组。 您必须重新分配此值以使 Angular 更改检测检测到此新数组:

filter(id) {
    this.reports = this.reports.filter(x => x.ID == id)[0] = {
      ID: 3233.0,
    ...
   }
}

https://stackoverflow.com/questions/64580571/

相关文章:

vim - 如何在没有重新映射的情况下仅使用一个命令在 Neovim 的新选项卡中打开终端?

python - 为什么我的 Django 表单不是 "valid"?无法获取更新数据库的 POST

python - 使用 boto3 获取新创建的亚马逊 ec2 实例的公共(public) IPv4

r - 匹配和替换字符向量中的单词

python - 是否可以设置 lint 自定义设置并忽略 pylance

assembly - 如何将浮点常量移动到 FP 寄存器中?

c - 可能知道变量所在的内存部分?

kubernetes - 如何在我的 configmap.yaml (Helm) 中使用 json

reactjs - 在 Next.js 中替换查询中的值

algorithm - 如果这个更简单、更快的算法有效,为什么我们需要 Dijkstra 算法?