0
I have a *ngFor that fills my html, and this data is fetched from an API, so I use Schedule to reload the data every time it gives seconds = 15.
Only that the data is being placed below the old la in HTML.
HTML:
<p [class]="mainColor + ' teste' " *ngFor="let element of fil">Filial {{ element.FI }} = {{ element.porc }}</p>
TS:
ngOnInit() {
schedule.scheduleJob(' 15 * * * * * ', () => {
this.MetaService.FiliaisMetaDiarias().subscribe(
data => {
const response = (data as any)
this.objeto_retorno = JSON.parse(response._body);
this.objeto_retorno.forEach(element => {
this.tots = element.TOTAL
element.TOTAL = (element.TOTAL * 100).toFixed(3) + '%'
element.TOTAL = element.TOTAL.toString().replace(".", ",")
if (this.tots >= this.MetaAtingida) {
this.fil.push({
FI: element.FILIAL,
porc: element.TOTAL
});
this.mainColor = 'MetaAtingida'
}
});
}
Every time the second is = 15 it code is reloaded and put into html, but not by deleting the previous data that is in html.
Somebody knows how to fix this?
The behavior, it seems to me, is due to your
this.fil.push()
if you are replacing the elements, before you should clean it.– Leandro Angelo
I clean the array, but still it remains the same
– Maria
Where you clean?
– Leandro Angelo
when starting the Schedule function
– Maria
It was better to use the filter and map method than foreach
– Eduardo Vargas