I have this html code which works great:
<button class="button-style standard-button button-blue" (click)="onOkClick()"
[ngClass]="{'disabled': !hasUnsavedNotes()}" [disabled]="!hasUnsavedNotes()">
Save and close
</button>
My question is how do I change class="button-style standard-button button-blue" to class="button-style standard-button button-grey" when hasUnsavedNotes() returns false? Or simply how do I change the button back color when its disabled? Thanks.
You can just do this little adjustment:
<button
[class.button-blue]="!hasUnsavedNotes()"
[class.button-grey]="hasUnsavedNotes()"
class="button-style standard-button"
(click)="onOkClick()"
[disabled]="!hasUnsavedNotes()">
Save and close
</button>
[class.button-blue]="!hasUnsavedNotes()"
will add button-blue
css class when !hasUnsavedNotes()
returns true
and will remove this class when !hasUnsavedNotes()
returns false
. Is the same to:
[class.button-grey]="hasUnsavedNotes()"
You can remove the [disabled] directive if you wish.
Here is a helpful list of "tricks" to use in Angular: Angular - Cheat Sheet
[ngClass]="{'button-blue': hasUnsavedNotes(), 'button-grey': !hasUnsavedNotes()}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With