Looking for guidance on Angular - color change on button click.
The loop binding is functioning well with dynamic variable display in an outer element like {{'profile3.q2_' + (i+1) | translate}}, but facing issues with [ngClass] variable binding.
How can I use the " [ngClass]='q2_' + (i+1) =='1'? ~~~ " part effectively?
Your help is appreciated.
*.html file
If not using dynamic variables:
<ion-row class="ion-margin-top ion-margin-bottom">
<ion-col size="4">
<ion-button expand="block" fill="outline"
[ngClass]="'q2_1 =='1'?'btn-background' : 'btn-default'" (click)="onClicked(1)">
<span class="contentText">{{'profile3.q2_1' | translate}}</span>
</ion-button>
</ion-col>
<ion-col size="4">
<ion-button expand="block" fill="outline"
[ngClass]="'q2_2 =='1'?'btn-background' : 'btn-default'" (click)="onClicked(2)">
<span class="contentText">{{'profile3.q2_2'| translate}}</span>
</ion-button>
</ion-col>
</ion-row>
~~
Preferred approach (using dynamic variable and condition):
<ion-row class="ion-margin-top ion-margin-bottom">
<ion-col size="4" *ngFor='let in of counter(7) ;let i = index'>
<ion-button expand="block" fill="outline"
[ngClass]="'q2_' + (i+1) =='1'?'btn-background' : 'btn-default'" (click)="onClicked(i+1)">
<span class="contentText">{{'profile3.q2_' + (i+1) | translate}}</span>
</ion-button>
</ion-col>
</ion-row>
*.ts file :
public q2_1 : string = "0";
public q2_2 : string = "0";
~~
counter(i: number) {
return new Array(i);
}
onClicked(opt){
if (opt == 1) this.q2_1=this.q2_1=='0'? '1': "0";
if (opt == 2) this.q2_2=this.q2_2=='0'? '1': "0";
~~~~
}
...
------------------------