gpt4 book ai didi

javascript - 无法在 Angular 2 中从父组件更新子组件 View

转载 作者:太空宇宙 更新时间:2023-11-04 15:53:45 25 4
gpt4 key购买 nike

朋克:Here

我有一个用于渲染的子组件 chartist.component chartist charts

在我的父组件 app.ts 中,我调用 chartist.component 两次来渲染折线图和条形图

在父组件中,我有按钮来刷新折线图和条形图,它们只是更改数据变量中的数据,然后传递给子组件以呈现图表。

我的问题是,图表仅在单击刷新按钮时更新一次。我想在每次数据对象发生变化时更新图表。

app.ts(父组件):

import {Component, NgModule, OnInit, AfterViewInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {ChartistChart} from './chartist.component'

@Component({
selector: 'my-app',
templateUrl: './src/app.html',,
})
export class App implements OnInit, AfterViewInit {
name:string;
data: any;
isFirst:true;
dataIndex:number;
allData:any;

constructor() {
this.name = 'Angular2'
this.dataIndex=0;
this.allData=[{chart data1},{chart data 2}];
}

ngOnInit() {
this.getAllCharts();
}

ngAfterViewInit() {}

public getAllCharts(){
this.data= this.allData[this.dataIndex];
return this.allData[this.dataIndex];
}

public updateLineChart(){
this.isFirst = !this.isFirst;
this.dataIndex = this.isFirst?0:1;
this.data.simpleLineData = this.allData[this.dataIndex].simpleLineData;
this.data.simpleLineOptions = this.allData[this.dataIndex].simpleLineOptions;
}

public updateBarChart(){
this.isFirst = !this.isFirst;
this.dataIndex = this.isFirst?0:1;
this.data.simpleBarData = this.allData[this.dataIndex].simpleBarData;
this.data.simpleBarOptions = this.allData[this.dataIndex].simpleBarOptions;
}
}

@NgModule({
imports: [ BrowserModule ],
declarations: [ App, ChartistChart ],
bootstrap: [ App ]
})
export class AppModule {}

app.html:

<div style="width:100%; height:350px">
<button (click)="updateLineChart()">Update Line chart</button> DataIndex: {{dataIndex}}
<chartist-chart *ngIf="data" chartistChartClass="ct-chart" chartistChartType="Line" [chartistChartData]="data['simpleLineData']"
[chartistChartOptions]="data['simpleLineOptions']">
</chartist-chart>
</div>
<br/> <br/>
<div style="width:100%; height:350px">
<button (click)="updateBarChart()">Update Bar chart</button> DataIndex: {{dataIndex}}
<chartist-chart *ngIf="data" chartistChartClass="ct-chart" chartistChartType="Bar" [chartistChartData]="data['simpleBarData']"
[chartistChartOptions]="data['simpleBarOptions']">
</chartist-chart>
</div>

chartist.component.ts(子):

import {
Component, ViewChild, Input, Output, ElementRef, EventEmitter, ChangeDetectorRef
} from '@angular/core';



@Component({
selector: 'chartist-chart',
templateUrl: './src/chartistChart.html',
providers: [],
})
export class ChartistChart {

@Input() chartistChartType: string;
@Input() chartistChartData: Object;
@Input() chartistChartOptions: Object;
@Input() chartistChartResponsive: Object;
@Input() chartistChartClass: string;
@Output() onChartReady = new EventEmitter<any>();

@ViewChild('chartistChart') public _selector: ElementRef;

private chart;
constructor(private cdr: ChangeDetectorRef){}

ngAfterViewInit() {
console.log('chart init', this.chartistChartData)
let _self = this;
_self.chart = new Chartist[_self.chartistChartType](_self._selector.nativeElement,
_self.chartistChartData,
_self.chartistChartOptions);

_self.onChartReady.emit(_self.chart);
this.cdr.detectChanges();
}



ngOnChanges(changes) {
if (this.chart) {
console.log('chartistChartData changed: ',this.chartistChartData);
(<any>this.chart).update(this.chartistChartData, this.chartistChartOptions);
}
}

ngOnDestroy(): void {
if (this.chart) {
this.chart.detach();
}
}
}

chartistChart.html:

<div #chartistChart class="{{chartistChartClass || ''}}"></div>

最佳答案

您需要做的就是在每次点击按钮时手动调用 this.getAllCharts() ,以便将 this.data 重新分配给新的数据系列:

public updateLineChart() {
this.dataIndex = this.dataIndex ? 0 : 1;
this.getAllCharts();
}

public updateBarChart() {
this.dataIndex = this.dataIndex ? 0 : 1;
this.getAllCharts();
}

你也可以去掉多余的isFirst标志,dataIndex就足够了。而且您也不需要手动设置 this.data.simpleBarDatathis.data.simpleLineData,因为它已经作为模板中的 props 传递。

演示: https://plnkr.co/edit/1eyTe5WYFAjL9ssRgdiS?p=preview

关于javascript - 无法在 Angular 2 中从父组件更新子组件 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42886061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com