You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
P2The issue is important to a large percentage of users, with a workaroundarea: material/buttonfeatureLabel used to distinguish feature request from other issues
Please describe the feature you would like to request.
I'm using MatButton directive as attribute like this: <button mat-button buttonType="{{button.type}}></button>"
I want to change 'mat-button' directive according to 'button.type', for example if 'button.type' is 'stroked' then 'mat-button' changed to 'mat-stroked-button'. How can to change type of button dynamicaly?
What is the use-case or motivation for this proposal?
Is there anything else we should know?
Reacted by Kim Kern, Adam, Manoj, fatalcaron, Osman Yavuz, Cédric Malard, Reda, Maryna Semeniuk, Oussama, Robin (Robert) Thomas and 26 more
I've had a similar question at stackoverflow, but nobody had a better idea... I understand why it can't be done, but it's sad I have to make 2 buttons all the time.
Reacted by mfarhani, Talita Gomes, Tomas, Adrian Cerbaro, Roman Tokmin, Sergiu Bologa and Maichel van Roessel
<button mat-button [ngClass]="condition ? 'mat-raised-button' : 'mat-stroked-button'">Button</button>
You can also use 'mat-flat-button' instead of 'mat-raised-button' if you don't need extra styling such as shadow.
Reacted by jjbenitez026, Adam, Sany18, AndreiAstanei, Andrey Gorkovenko, Kevin Qu, Sylvain Dumont, Palagesiu Cezar Ioan, Ali Celebi, Bipul Bikram Thapa and 6 more
Reacted by Jonas Vercammen, Zunayed Shahriar, Ali Celebi, Nitai Charan and Bipul Bikram Thapa
<button mat-button [ngClass]="condition ? 'mat-raised-button' : 'mat-stroked-button'">Button</button>
You can also use 'mat-flat-button' instead of 'mat-raised-button' if you don't need extra styling such as shadow.
But it doesn't really work due to .mat-button having more precedence on color and causing various other issues, which comes from theming.scss within Material:
So I guess I have to go back to my solution which is even more problematic as I need to dynamically support <a> or <button> so have to duplicate all of that again.
My dynamic buttons/anchors from CMS were at 36 LOC but since I can't do that, I'm back to 200 LOC to achieve it since I need to switch over the variants for both anchors and buttons
P2The issue is important to a large percentage of users, with a workaroundarea: material/buttonfeatureLabel used to distinguish feature request from other issues
Activity
Shamim56 commentedon Mar 3, 2019
you could just use the render 2 library remove/set attribute library for this
edit: cant actually do this, just use
mfarhani commentedon Mar 3, 2019
If i using setAttribute for this purpose, what should place instead value?
renderer.setAttribute(elRef,'mat-stroked-button','value')Shamim56 commentedon Mar 4, 2019
it seems this cant actually be done using setAttribute since mat-button seems to be a directive
why not just use ngIf, this is usually the most common way to address something like this
Totati commentedon Mar 9, 2019
I've had a similar question at stackoverflow, but nobody had a better idea... I understand why it can't be done, but it's sad I have to make 2 buttons all the time.
intellix commentedon Mar 26, 2019
I'm using a CMS to allow adding buttons dynamically and for the button.type I'm doing something like this:
mfarhani commentedon Mar 26, 2019
Thanks @intellix , i wrote some code similar this.
maxpopovitch commentedon Jul 14, 2020
<button mat-button [ngClass]="condition ? 'mat-raised-button' : 'mat-stroked-button'">Button</button>You can also use 'mat-flat-button' instead of 'mat-raised-button' if you don't need extra styling such as shadow.
amakhrov commentedon Aug 15, 2020
Dynamically setting the class works currently just because, based on the button source code (https://github.com/angular/components/blob/master/src/material/button/button.ts#L105) it's pretty much everything that those different attributes (mat-raised-button, mat-stroke-button, etc) do.
However, it's not a part of the official mat button api - so this can break in future.
I'm wondering if it would make sense to introduce a new input for a button variant (similar to
variantprop in React's Material implementation: https://material-ui.com/components/buttons/) and deprecate existing attributes over time? For instance, we already have a singlecolorinput instead of relying on multiplemat-color-primary/mat-color-accentattributes.vjonas commentedon Feb 24, 2021
amazing, just what I was looking for. thank you!
intellix commentedon May 13, 2021
The idea of doing this is nice:
But it doesn't really work due to .mat-button having more precedence on color and causing various other issues, which comes from theming.scss within Material:
So I guess I have to go back to my solution which is even more problematic as I need to dynamically support
<a>or<button>so have to duplicate all of that again.My dynamic buttons/anchors from CMS were at 36 LOC but since I can't do that, I'm back to 200 LOC to achieve it since I need to switch over the variants for both anchors and buttons
angular-robot commentedon Feb 21, 2022
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.
Find more details about Angular's feature request process in our documentation.
19 remaining items
Ac1d0n3 commentedon Apr 30, 2024
Cheers I also had this issue ... if you only want to modify the mat-button to stroked flat raised ... this works fine
`import { Directive, ElementRef, Input, Renderer2, inject } from '@angular/core';
import { BnButtonType } from '../../interfaces/src/bn-button-type';
import { BnRendererUtil } from '@binom/sdk-utils/renderer';
@directive({
selector: 'button[bnMatbutType]',
exportAs: "bnMatbutType",
standalone: true,
})
export class BnMatbutTypeDirective {
private renderEl: BnRendererUtil;
private renderer = inject(Renderer2);
private el = inject(ElementRef);
@input() set bnMatbutType(val:BnButtonType){
this.renderEl.removeClasses([
'mat-mdc-button',
'mdc-button--unelevated', 'mat-mdc-unelevated-button',
'mdc-button--raised', 'mat-mdc-raised-button',
'mdc-button--outlined', 'mat-mdc-outlined-button']);
}
constructor() { this.renderEl = new BnRendererUtil(this.renderer, this.el); }`
... <button mat-button color="accent" [bnMatbutType]="butType" ...
danielschmitz commentedon Oct 3, 2024
#15367 (comment) solution works fine
antoniopinarella commentedon Oct 4, 2024
the best solution with ngClass would be to switch over: HOST_SELECTOR_MDC_CLASS_PAIR
but its not exported..
feat(material/button): allow apperance to be set dynamically
feat(material/button): allow appearance to be set dynamically
feat(material/button): allow appearance to be set dynamically
feat(material/button): allow appearance to be set dynamically
CodyTolene commentedon Mar 14, 2025
👍👍
angular-automatic-lock-bot commentedon Apr 14, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.