0

I wanted to change mat-button type dynamically based on certain conditions. This requires changing an attribute of <button> element dynamically. What is the best way to do that? Below is a stackblitz with basic setup.

https://stackblitz.com/edit/angular-material-change-btn-type

I've seen this post. But there are total 8 button types and I think probably it won't be a good solution to add 8 conditions and bind them to a variable. Is there any better alternative?

4
  • Possible duplicate of How to add conditional attribute in Angular 2? Commented May 14, 2019 at 9:00
  • Not really. There are 8 button types available. And i don't think it would be a good solution to add 8 conditions for them. Instead I am seeking for an option to bind a variable to element itself in the form of attribute Commented May 14, 2019 at 9:03
  • This is not supported. stackoverflow.com/questions/37489761/… Commented May 14, 2019 at 9:05
  • Thanks @MuhammetCanTONBUL. But is there any better approach/work-around to solve this problem? Commented May 14, 2019 at 9:07

1 Answer 1

3

You should be able to solve this by just changing the button element in your template to:

<button mat-button [ngClass]="selectedButtonType">This Button </button>

I hope that helps!

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

1 Comment

Oh, that's pretty useful. I didn't know that it works with classes as well. Thank you so much for help.

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.