I am trying to create a chart in angular and I get data using a service. I want to use the array data get from here. pieData: any = []; The console inside the constructor show the data correctly. but the console inside the ngOnInit shows empty array.
import { Component, OnInit } from '@angular/core';
import { PieChartService } from './pie-chart.service';
@Component({
selector: 'app-pie-chart',
templateUrl: './pie-chart.component.html',
styleUrls: ['./pie-chart.component.css']
})
export class PieChartComponent implements OnInit {
pieData: any = [];
constructor(private PieChart: PieChartService) {
this.PieChart.getMethod().subscribe((res: any) => {
this.pieData = res.pieData;
console.log(this.pieData);
})
}
ngOnInit() { }
//code for create a graph here
}