I am facing an issue with retrieving the value of countOnProgress from my property. The problem is that I can successfully get the value of countOnProgress when I subscribe to it, but outside of the subscription, countOnProgress returns 0. This means that I can't use countOnProgress in progressLastYear function. How can I set the value of countOnProgress to retain the subscribed value without it returning to 0?
import { Component, OnInit, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { DashboardService } from './dashboard.service';
import { Observable, of, timer } from 'rxjs';
import 'rxjs/add/operator/takeWhile';
import 'rxjs/add/observable/timer';
@Component({
templateUrl: 'dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
alive = true;
countOnProgress:number = 0;
max: number = 200;
value: number = 100;
stacked: any[] = [];
constructor(@Inject (DashboardService) private dashboardService: DashboardService){
}
ngOnInit(): void {
Observable.timer(0, 30000)
.takeWhile(() => this.alive)
.subscribe(() => {
this.dashboardService.getCountProgress().subscribe(resp => {
this.countOnProgress = resp.d;
console.log(this.countOnProgress); //It found the data
})
});
this.progressLastYear();
}
progressLastYear(): void{
const types = ['success', 'info', 'warning', 'danger'];
const values = [this.countOnProgress];
console.log(values);
this.stacked = [];
for (let i = 0; i < 5; i++) {
this.stacked.push({
value: values[1],
type: types[i],
label: values[1]
});
console.log(this.stacked); //The datas: 0, succes, 0 (didnt get countOnProgress' value)
}
}
}
Thank you