How to change CSS when it's ng-disabled?

All we need is an easy explanation of the problem, so here it is.

I have this button:

<input type="submit" value="@Translator.Translate("PAYOUT")"
     class="btn-block secondary-button save-changes padding-8"
     ng-disabled="PayoutEnabled==false" ng-click="PayOut()" />

But even when it’s disabled it has the same class as it’s enabled, just not clickable. I want to change background when it’s disabled so that user can see that button, is disabled. How can I do that? Do I need some ng-disabled CSS class or there is some other way?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

What Toress answered should work fine but you don’t need the help of AngularJS here at all (a native implementation & usage is always best).

You can make use of CSS3 since you already have a class on it. Example:

input.save-changes {
    /* some style when the element is active */
}

input.save-changes[disabled] {
    /* styles when the element is disabled */
    background-color: #ddd;
}

Edit: You can immediately test it on this page of StackOverflow. Just inspect the blue button element and put the disabled attribute and see it’s CSS.

.save-changes {
  background-color: red;
  padding: 7px 13px;
  color: white;
  border: 1px solid red;
  font-weight: bold;
}
.save-changes[disabled] {
  background-color: #FF85A1
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app ng-init="PayoutEnabled = true">
  <a href="#" rel="nofollow noreferrer noopener" ng-click="PayoutEnabled = !PayoutEnabled">
  {{PayoutEnabled ? 'Disable' : 'Enable'}} the below button</a>
  <br>
  <br>

  <input class="save-changes" type="submit" value="PAYOUT" ng-disabled="PayoutEnabled == false" />
</div>

Method 2

use ng-class

<input type="submit" value="@Translator.Translate("PAYOUT")" class="btn-block 
secondary-button save-changes padding-8" ng-disabled="PayoutEnabled==false" 
ng-click="PayOut()" ng-class="{'diabled-class': !PayoutEnabled}" />

this will add css class diabled-class to the input when PayoutEnabled is false (!PayoutEnabled is true).

Method 3

AngularJS adds pseudo-class disabled when ng-disabled is false so i think here is the simplest solution to refer to disabled button :

button:disabled {
    color:#717782;
}

Method 4

In case you are using Bootstrap and a more recent Angular version than AngularJs you can ovverride the default style adding this to the styles.css file

.btn.disabled, .btn:disabled {
    opacity: .35 !important;
    background-color: gray !important;
}

The higher the opacity the darker or more solid the color will be.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply