Repeated data in html (ANGULAR)

0

I have a * ngFor that fills my html, and this data is fetched from an API, then I use schedule to reload the data every time it gives seconds = 15.

Only the data is being placed below the old one 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 is reloaded and placed in the html, but it is not deleting the previous data that is in html.

Does anyone know how to solve this?

    
asked by anonymous 22.08.2018 / 17:29

1 answer

1

See if this works.

ngOnInit() {
        schedule.scheduleJob(' 15 * * * * * ', () => {      
          this.MetaService.FiliaisMetaDiarias().subscribe(
  data => {
    const response = (data as any)
    this.objeto_retorno = JSON.parse(response._body);

    this.fil.length = 0;

    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'

      }
        });
    }

For some very strange reason, in JavaScript, you can "reset" an array by setting its size to 0.

var fil = [1,2,3];
//push esvaziar ou redefinir o tamanho.
fil.push(4);
console.log(fil);

//redefinindo o tamanho e fazendo novo push
fil.length = 0;
fil.push(5);

console.log(fil);
    
22.08.2018 / 17:55