Skip to content

Set button type dynamicaly #15367

Closed

Description

@mfarhani

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?

Activity

  1. Shamim56 commented on Mar 3, 2019

    @Shamim56

    you could just use the render 2 library remove/set attribute library for this

    edit: cant actually do this, just use

    <button *ngIf="!stroked" mat-button>Text</button>
    <button *ngIf="stroked" mat-button-stroked>Text</button>"
    
  2. mfarhani commented on Mar 3, 2019

    @mfarhani
    Author

    you could just use the render 2 library remove/set attribute library for this

    If i using setAttribute for this purpose, what should place instead value?
    renderer.setAttribute(elRef,'mat-stroked-button','value')

  3. Shamim56 commented on Mar 4, 2019

    @Shamim56

    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

    <button *ngIf="!stroked" mat-button>Text</button>
    <button *ngIf="stroked" mat-button-stroked>Text</button>"
  4. Totati commented on Mar 9, 2019

    @Totati
    Contributor

    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.

  5. intellix commented on Mar 26, 2019

    @intellix

    I'm using a CMS to allow adding buttons dynamically and for the button.type I'm doing something like this:

    export interface Button {
      id: string;
      text: string;
      type: string;
      color: string;
      routerLink: string;
    }
    <ng-container [ngSwitch]="data.type">
      <a *ngSwitchDefault mat-button [color]="data.color" [routerLink]="data.routerLink">{{ data.text }}</a>
      <a *ngSwitchCase="'raised'" mat-raised-button [color]="data.color" [routerLink]="data.routerLink">{{ data.text }}</a>
      <a *ngSwitchCase="'stroked'" mat-stroked-button [color]="data.color" [routerLink]="data.routerLink">{{ data.text }}</a>
      <a *ngSwitchCase="'flat'" mat-flat-button [color]="data.color" [routerLink]="data.routerLink">{{ data.text }}</a>
      <a *ngSwitchCase="'icon'" mat-icon-button [color]="data.color" [routerLink]="data.routerLink">{{ data.text }}</a>
      <a *ngSwitchCase="'fab'" mat-fab [color]="data.color" [routerLink]="data.routerLink">{{ data.text }}</a>
      <a *ngSwitchCase="'mini-fab'" mat-mini-fab [color]="data.color" [routerLink]="data.routerLink">{{ data.text }}</a>
    </ng-container>
  6. mfarhani commented on Mar 26, 2019

    @mfarhani
    Author

    Thanks @intellix , i wrote some code similar this.

  7. added
    feature
    P4
    on May 7, 2019
  8. maxpopovitch commented on Jul 14, 2020

    @maxpopovitch

    <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.

  9. amakhrov commented on Aug 15, 2020

    @amakhrov
    Contributor

    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 variant prop in React's Material implementation: https://material-ui.com/components/buttons/) and deprecate existing attributes over time? For instance, we already have a single color input instead of relying on multiple mat-color-primary / mat-color-accent attributes.

  10. vjonas commented on Feb 24, 2021

    @vjonas

    <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.

    amazing, just what I was looking for. thank you!

  11. intellix commented on May 13, 2021

    @intellix

    The idea of doing this is nice:

    <button mat-button class="mat-raised-button" color="primary">

    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:

    .mat-button.mat-primary {
      color: black;
    }
    
    .mat-raised-button {
      color: white;
    }

    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

  12. angular-robot commented on Feb 21, 2022

    @angular-robot
    Contributor

    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.

  13. 19 remaining items

  14. Ac1d0n3 commented on Apr 30, 2024

    @Ac1d0n3

    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']);

    let classes=[]
    
    switch(val){
      case 'stroked': 
        classes = ['mdc-button--outlined', 'mat-mdc-outlined-button']; break;
      case 'raised': 
        classes = ['mdc-button--raised', 'mat-mdc-raised-button']; break;
      case 'flat': 
        classes = ['mdc-button--unelevated', 'mat-mdc-unelevated-button']; break;
      case '': default:
        classes = ['mat-mdc-button']; break;
    }
    this.renderEl.addClasses(classes)
    

    }
    constructor() { this.renderEl = new BnRendererUtil(this.renderer, this.el); }`

    ... <button mat-button color="accent" [bnMatbutType]="butType" ...

  15. danielschmitz commented on Oct 3, 2024

    @danielschmitz

    Hi all, any updates on this yet? Just checking in

    #15367 (comment) solution works fine

  16. antoniopinarella commented on Oct 4, 2024

    @antoniopinarella

    the best solution with ngClass would be to switch over: HOST_SELECTOR_MDC_CLASS_PAIR
    but its not exported..

  17. added 2 commits that reference this issue on Mar 13, 2025
    81c5c74
    e89a2f7
  18. added
    P2
    and removed
    P4
    on Mar 13, 2025
  19. self-assigned this
    on Mar 13, 2025
  20. added a commit that references this issue on Mar 13, 2025
    f757c2f
  21. added a commit that references this issue on Mar 14, 2025
    e79f605
  22. CodyTolene commented on Mar 14, 2025

    @CodyTolene

    👍👍

  23. angular-automatic-lock-bot commented on Apr 14, 2025

    @angular-automatic-lock-bot

    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.

  24. locked and limited conversation to collaborators on Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2area: material/buttonfeature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions