1

I have a situation where i have multiple records and i used in that *ngFor loop , so per record there is one button And I am trying to do is onclick change background of button so that it looks alike it is clicked

<div class="main" *ngFor="let data of fetched_info; let i=index">
    <ion-grid no-padding no-margin>
        <ion-row no-padding no-margin class="row">
            <ion-col col-4 no-padding no-margin> 
                <button ion-button small icon-start color="secondary"
outline (click)="shortlist($event,data[i].username)" class="">
                    <ion-icon ios="ios-star-outline" md="md-star-outline"></ion-icon>
                    Shortlist
                </button>
            </ion-col>

        </ion-row>
    </ion-grid>
</div>

onclick of shortlist() function i want to make it happen

1 Answer 1

2

you can try like this

HTML

<div class="main" *ngFor="let data of fetched_info; let i=index">
 <ion-grid no-padding no-margin>
     <ion-row no-padding no-margin [ngClass]="backgroundColorFlag[i] == true ? 'backgroundColorClass' : 'backgroundColorNormalClass' " class="row">
       <ion-col col-4 no-padding no-margin> 
         <button ion-button small icon-start color="secondary"
           outline (click)="shortlist($event,data[i].username, i)" class="">
           <ion-icon ios="ios-star-outline" md="md-star-outline"></ion-icon>
            Shortlist
         </button>
       </ion-col>
     </ion-row>
 </ion-grid>
</div>

TS

    backgroundColorFlag: any[] = [];
    shortlist(data, i) {
      this.backgroundColorFlag[i] != this.backgroundColorFlag;
    }
    functionWhichHavefetched_info() {
     for (let i = 0; i < fetched_info.length; i++) {
           this.backgroundColorFlag[i] = false // default we are setting value false and on click we set this flag value true
      }
    }

you need to create a class like this backgroundColorClass and backgroundColorNormalClass and add or remove class by using a flag backgroundColorFlag i hope it helps you out

Sign up to request clarification or add additional context in comments.

5 Comments

No it is not helping .. in case i have 10 button so it will apply on all buttons i want to be applied it on only one button which is clicked
Hey I just edit the answer I create one Array of the flag and that array has the same length as fetched_info array have please check my updated answer
i was about to do the same and here you mentioned it ...well let me try this one
yeahhh sure :) let me know it is working or not :) if it is please accept my answer
yes its working . just one edit this.backgroundColorFlag[i] != this.backgroundColorFlag;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.