1
<ion-col col-3>
                <button ion-button (click)="onPunchPress($event)"><span>1</span></button>
            </ion-col>
            <ion-col col-3>
                <button ion-button (click)="onPunchPress($event)"><span>2</span></button>
            </ion-col>

How can I change color of specific button? Suppose If I click on 1st button then what to do that only its [color] attribute is changed from “light” to “danger” ?

I have tried so many way, when I use ngClass it changes class for all the buttons. I have to change of clicked one and it should be toggle click means change/revert on each button. How can I achieve it?

3
  • Posted on Ionic forum too : forum.ionicframework.com/t/… Commented Oct 27, 2017 at 5:36
  • Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. Commented Oct 27, 2017 at 12:00
  • Thanks for being honest about your cross-posting. It is marked as solved over there, so perhaps you may need to clarify here what part is not solved. If you could also link from there to here, that would be good - undeclared cross-posting is a great way to waste people's time (since they spend time on giving help that the OP no longer needs). Commented Oct 27, 2017 at 12:02

1 Answer 1

1

You can achieve it by [style.background]="boolFlag ? 'blue' : 'red'", and toggling the flag on click event., like : (click)="boolFlag = !boolFlag "

See this Plunker for example

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

4 Comments

I am puzzled why this is not working on mine side! <button class="custom-button" ion-button (click)="boolFlag1 = !boolFlag1" [style.background]="boolFlag1 ? 'danger' : 'light'"><span>1</span></button>
this should work.., could you post your error scenario online on any editor like plunker?
Got it, danger and light are not colors they are just color varibales. We can use only color names or codes.
use actual color names, to use as property remove single qoute

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.