I am trying to include some data as string in one of my access modifiers (public pieChartLabels) which is an array. I've been checking with console.log what I was getting from my provider, and it works fine, but I just can't make it work when I try with the modifiers.
I have understood that functions such as ionViewDidLoad() are executed until the modifiers are ready to be used, therefore I used my provider inside the array to get JSON data.
JSON:
{
"name": "progress",
"overall": [
"30",
"5",
"3",
"62"
]
}
HTML:
<ion-content padding class="ionContent" *ngFor="let p of allProducts">
<div style="display: block" class="pieChart">
<canvas baseChart height="50%" width="50%"
[data]="p.overall"
[labels]= "pieChartLabels"> <!--I want to include a number after the label-->
</canvas>
</div>
</ion-content>
TypeScript:
export class LoginPage {
public allProducts = [] as any;
public percentages = [] as any;
public newPercentages = [] as any;
constructor(public navCtrl: NavController, public navParams: NavParams, private screenOrientation: ScreenOrientation, private dataProvider: DataProvider) {
}
ionViewDidLoad() { // As soon as the page is loaded this executes
this.dataProvider.getProducts()
.subscribe((Response: Array<any>) => {
this.allProducts = [Response]; // Getting data as array for HTML
this.percentages = Response; // Getting data as object
console.log("First", this.allProducts);
console.log("Second", this.percentages.name);
});
}
ionViewDidEnter(){ // Executes after ionViewDidLoad()
console.log("I want this: ", JSON.stringify(`${this.percentages.overall[0]}`));
console.log("Final Test: ",JSON.stringify(`${this.dataProvider
.getProducts().subscribe((Response: Array<any>) => {
this.newPercentages = Response;}),this.newPercentages.name}`),this.newPercentages.overall)
}
public pieChartLabels:string[] = [ // <-----HERE IS MY PROBLEM!
'Propuestas Originales Cumplidas'+JSON.stringify(`${this.dataProvider
.getProducts().subscribe((Response: Array<any>) => {
this.finalPercentages = Response;}),this.finalPercentages.overall[0]}`)+'%',
}
What I get in the console.log is exactly what I expect:
[app-scripts] [01:46:16] console.log: First [object Object]
[app-scripts] [01:46:16] console.log: Second progress
[app-scripts] [01:46:16] console.log: I want this: "30"
[app-scripts] [01:46:16] console.log: Final Test: "progress" 30,5,3,62
But what I get in my simulator is "Undefined", as if the value of finalPercentages wasn't stored.
The output that I want is "Propuestas Originales Cumplidas 30%".
What can I do? Also, is there a way to combine it in HTML instead? Something like [labels]="source1,source2"?