# Dynamic button color

**URL:** <https://forum.ionicframework.com/t/dynamic-button-color/46732>\
**Category:** ionic-v3\
**Created:** [March 17, 2016, 8:15pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732 "2016-03-17T20:15:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![polska03](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@polska03](https://forum.ionicframework.com/u/polska03)\
**Post date:** [March 17, 2016, 8:15pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/1 "2016-03-17T20:15:12Z")

</div>

I have a button:

`<button>My Buttont</button>`

I need to conditional change the color of it. For example:

`<button if var < 20 = Primary if var >= 21 Danger>My Buttont</button>`

How can I do this?

---

<div class="post-metadata">

**Author:** ![ChrisGriiffith](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/chrisgriiffith/32/66576_2.png) [@ChrisGriiffith](https://forum.ionicframework.com/u/ChrisGriiffith)\
**Post date:** [March 17, 2016, 8:54pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/2 "2016-03-17T20:54:33Z")

</div>

Look at ngclass ([https://docs.angularjs.org/api/ng/directive/ngClass](https://docs.angularjs.org/api/ng/directive/ngClass)) to help you along. If you are using Ionic2, you will have to find the Angular2 version

---

<div class="post-metadata">

**Author:** ![polska03](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@polska03](https://forum.ionicframework.com/u/polska03)\
**Post date:** [March 17, 2016, 9:00pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/3 "2016-03-17T21:00:58Z")

</div>

Thanks for the reply, but I don’t think we can use it like that:

`<button Danger [ngClass]="{active: Primary, disabled: Primary, hover: Primary}" >MyButton</button>`

This does not work.

---

<div class="post-metadata">

**Author:** ![xr0master](https://avatars.discourse-cdn.com/v4/letter/x/b4bc9f/32.png) [@xr0master](https://forum.ionicframework.com/u/xr0master)\
**Post date:** [March 17, 2016, 9:25pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/4 "2016-03-17T21:25:28Z")

</div>

o\_0  
pseudo-code:  
`<button [ngClass]="{Primary: var < 20, Danger: var >= 21}" >MyButton</button>`

---

<div class="post-metadata">

**Author:** ![polska03](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@polska03](https://forum.ionicframework.com/u/polska03)\
**Post date:** [March 17, 2016, 9:43pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/5 "2016-03-17T21:43:08Z")

</div>

I have just tried this, but it is not working for me.

`<button [ngClass]="{Primary: bl < 20, Danger: bl >= 21}" >MyButton</button>`

Then inside my consructor:

`this.bl = 100;`

So it should be a red button, but it is blue.

---

<div class="post-metadata">

**Author:** ![xr0master](https://avatars.discourse-cdn.com/v4/letter/x/b4bc9f/32.png) [@xr0master](https://forum.ionicframework.com/u/xr0master)\
**Post date:** [March 17, 2016, 10:01pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/6 "2016-03-17T22:01:01Z")

</div>

oh man, because you need use really classes. Something like:

```auto
<button [ngClass]="{'button-danger': bl >= 21}" >MyButton</button>

```

I am strongly recommend you learn Angular before.

---

<div class="post-metadata">

**Author:** ![polska03](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@polska03](https://forum.ionicframework.com/u/polska03)\
**Post date:** [March 17, 2016, 10:18pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/7 "2016-03-17T22:18:13Z")

</div>

WORKS! Thank you so much!

---

<div class="post-metadata">

**Author:** ![iignatov](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/iignatov/32/100112_2.png) [@iignatov](https://forum.ionicframework.com/u/iignatov)\
**Post date:** [March 17, 2016, 11:04pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/8 "2016-03-17T23:04:27Z")

</div>

In Angular 2 could be written this way as well:

```auto
<button [class.button-danger]="bl >= 21">MyButton</button>

```

---

<div class="post-metadata">

**Author:** ![tuongaz](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@tuongaz](https://forum.ionicframework.com/u/tuongaz)\
**Post date:** [March 24, 2016, 12:34am UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/9 "2016-03-24T00:34:08Z")

</div>

I tried this but it doesn’t seem to work:

`<button [ngClass]="['button-fab-bottom','button-fab','button-small','button-danger','button-fab-left']"`

If I use this:

`<button fab-bottom fab small danger fab-left>Send</button>` it works. But how do I dynamically add `fab-bottom fab small danger fab-left`

---

<div class="post-metadata">

**Author:** ![xr0master](https://avatars.discourse-cdn.com/v4/letter/x/b4bc9f/32.png) [@xr0master](https://forum.ionicframework.com/u/xr0master)\
**Post date:** [March 24, 2016, 6:26am UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/10 "2016-03-24T06:26:39Z")

</div>

you are missing conditions

---

<div class="post-metadata">

**Author:** ![tuongaz](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@tuongaz](https://forum.ionicframework.com/u/tuongaz)\
**Post date:** [March 24, 2016, 11:46am UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/11 "2016-03-24T11:46:56Z")

</div>

I tried with conditions already, it doesn’t help.

---

<div class="post-metadata">

**Author:** ![iignatov](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/iignatov/32/100112_2.png) [@iignatov](https://forum.ionicframework.com/u/iignatov)\
**Post date:** [March 24, 2016, 2:43pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/12 "2016-03-24T14:43:51Z")

</div>

I don’t understand why would you want to apply all these classes dynamically. In case that you _really need all of them_ added/removed from the button then consider using two different buttons instead - one without the classes and one with all of them - and then you can hide/show the appropriate button based on some condition. If this doesn’t help, could you provide some code that demonstrates what exactly are you trying to achieve?

---

<div class="post-metadata">

**Author:** ![tuongaz](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@tuongaz](https://forum.ionicframework.com/u/tuongaz)\
**Post date:** [March 29, 2016, 12:48am UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/13 "2016-03-29T00:48:00Z")

</div>

Thanks. The reason i want it dynamically is because I want to provide a FAB button, and user can choose whether the button is displayed on Left, Right or Center of the app.

---

<div class="post-metadata">

**Author:** ![iignatov](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/iignatov/32/100112_2.png) [@iignatov](https://forum.ionicframework.com/u/iignatov)\
**Post date:** [March 30, 2016, 7:20am UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/14 "2016-03-30T07:20:46Z")

</div>

@tuongaz It seems that just setting the CSS class doesn’t work for FAB buttons, e.g. adding `button-fab-right` doesn’t move the button to the right.

Therefore I would suggest you to use something like `ngSwitch` to change the position depending on a property `position` that you set in your code as needed:

```auto
    <div [ngSwitch]="position">
      <button *ngSwitchWhen="'left'" fab-bottom fab small danger fab-left>Send</button>
      <button *ngSwitchWhen="'center'" fab-bottom fab small danger fab-center>Send</button>
      <button *ngSwitchWhen="'right'" fab-bottom fab small danger fab-right>Send</button>
    </div>

```

Here is a [working demo](http://plnkr.co/edit/ObeGlf?p=preview).

---

<div class="post-metadata">

**Author:** ![fredy26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fredy26/32/92767_2.png) [@fredy26](https://forum.ionicframework.com/u/fredy26)\
**Post date:** [April 14, 2016, 2:50pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/15 "2016-04-14T14:50:01Z")

</div>

Not working in Ionic 2 for some reason…

```
<ion-badge [class.bonus]="item.type === '+'" item-right>
                {{item.type}} {{item.amount}} мин.
</ion-badge>

```

Neither works this piece of code:

```
<ion-badge [ngClass]="{'bonus' : item.type === '+''}" item-right>
                {{item.type}} {{item.amount}} мин.
</ion-badge>
```

---

<div class="post-metadata">

**Author:** ![iignatov](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/iignatov/32/100112_2.png) [@iignatov](https://forum.ionicframework.com/u/iignatov)\
**Post date:** [April 14, 2016, 3:10pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/16 "2016-04-14T15:10:38Z")

</div>

@fredy26 Both examples are working for me with `beta.4`, see [this demo plunker](http://plnkr.co/edit/QHzQYxYiDRHIKez0Eh2C?p=preview).

---

<div class="post-metadata">

**Author:** ![fredy26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fredy26/32/92767_2.png) [@fredy26](https://forum.ionicframework.com/u/fredy26)\
**Post date:** [April 14, 2016, 3:15pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/17 "2016-04-14T15:15:42Z")

</div>

It may be so. I’m still using beta.3.  
Do you have any experience upgrading to beta.4?  
I’m affraid that my code will break after upgrade…

Here is my system info:

> Cordova CLI: 6.1.1  
> Gulp version: CLI version 1.2.1  
> Gulp local: Local version 3.9.1  
> Ionic Framework Version: 2.0.0-beta.3  
> Ionic CLI Version: 2.0.0-beta.24  
> Ionic App Lib Version: 2.0.0-beta.14  
> ios-deploy version: Not installed  
> ios-sim version: 5.0.6  
> OS: Mac OS X El Capitan  
> Node Version: v5.7.1  
> Xcode version: Xcode 7.3 Build version 7D175

---

<div class="post-metadata">

**Author:** ![iignatov](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/iignatov/32/100112_2.png) [@iignatov](https://forum.ionicframework.com/u/iignatov)\
**Post date:** [April 14, 2016, 3:45pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/18 "2016-04-14T15:45:31Z")

</div>

@fredy26 I recently upgraded my project to `beta.4` almost without problems _(I just had forgotten to update one file)_. I think that you shouldn’t have any serious problems if you follow [this upgrade guide](https://forum.ionicframework.com/t/ionic-2-projects-updating-to-beta-4/49054). Or if you prefer the safe way - you could make a copy of your project folder _(or a branch if you’re using VCS)_ and perform the upgrade there to see if you’ll hit any problems.

---

<div class="post-metadata">

**Author:** ![fredy26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fredy26/32/92767_2.png) [@fredy26](https://forum.ionicframework.com/u/fredy26)\
**Post date:** [April 14, 2016, 3:55pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/19 "2016-04-14T15:55:40Z")

</div>

Whoa! Thanks. I just upgraded the copy of my project by changing package.json version of ionic-angular and angular (and related dependencies also).  
`[ngClass]` is not working yet (and floating placeholders on input fields stopped working)  
As the next step - I will try to follow all the steps from your guide.  
Thanks a lot! Great job you’ve done.

---

<div class="post-metadata">

**Author:** ![fredy26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fredy26/32/92767_2.png) [@fredy26](https://forum.ionicframework.com/u/fredy26)\
**Post date:** [April 14, 2016, 4:10pm UTC](https://forum.ionicframework.com/t/dynamic-button-color/46732/20 "2016-04-14T16:10:17Z")

</div>

Nope. Still not working. Looks like a problem was not in Ionic version.  
Maybe I should import ngClass as a directive?

[Next page](https://forum.ionicframework.com/t/dynamic-button-color/46732.md?page=2)
