How to display and hide Toogle within ngfor correctly?

1

listNotas() {
    
    this.service.getList(this.idAluno).subscribe(
      data => {
        this.error = data.error;
        this.list = data.message;        
        console.log(this.list);        
      },
      err => console.log(err)
    );
  }

status:Boolean=false;
  myToggle(i){
    this.status[i] = true;
    console.log(this.status[i]);    
  }
<div *ngFor="let data of list; let i = index">
    <span>
      {{data.aula}}
      <ion-toggle (click)="myToggle(i)"></ion-toggle>
    </span>
    <span *ngIf="status == true">
      {{data.nome}}
    </span>
  </div>
    
asked by anonymous 19.07.2018 / 00:49

2 answers

1

The best way to do this would be:

First map to list for each element to have a status of false

array = array.map(obj = > { obj.status=false; return obj; }) 

Then your toggle function might look like this:

myToggle(element){
  element.active = !element.active;    
}

finally no html

<div *ngFor="let data of list">
    <span>
      {{data.aula}}
      <ion-toggle (click)="myToggle(data)"></ion-toggle>
    </span>
    <span *ngIf="data.status == true">
      {{data.nome}}
    </span>
  </div>
    
19.07.2018 / 15:44
0

I was able to solve it and I'll leave the solution posted here:

status:Boolean=false;
  myToggle(data){
    if(data.status === 1){
      data.status = 0;
    }else{
      data.status = 1;
    }    
    console.log(data.status)    
  }
<div *ngFor="let data of list">
    <span>
      {{data.aula}} - {{data.status}}
      <button ion-button (click)="myToggle(data)">mostrar</button>
    </span>
    <span *ngIf="data.status == 0">
      {{data.aula}}
    </span>
  </div>
for some reason the <ion-toggle> button did not work ...     
20.07.2018 / 02:02