42

This is my mat-button:

<button class="example-20-width" mat-raised-button disabled>Edit Client</button>

How can I control it and make it disabled or not based on whether a select form is emtry or not?

Here is my field form:

<mat-form-field class="example-full-width">
  <mat-select  placeholder="Select customer">
    <mat-option *ngFor="let food of books.data" 
        [value]="food.company">
      {{food.company}}
    </mat-option>
    <mat-option>
    </mat-option>
  </mat-select>
</mat-form-field>
Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
Cap Barracudas
  • 2,347
  • 4
  • 23
  • 54

6 Answers6

57

If you look at Angular Material Demos (button), which is an older version of Angular Material demo, there is a button performing this.

This demo used to correspond (it's now outdated) to the demo on the Angular GitHub page, see: github.com - Angular Material - src/demo-app/button.

You can simply use:

<button mat-button [disabled]="isDisabled">

where isDisabled is a boolean define in your component.

Yun
  • 3,056
  • 6
  • 9
  • 28
moi_meme
  • 9,180
  • 4
  • 44
  • 63
  • 1
    This works here, but i the button for material do not show disabled style. When I view source this becomes ng-reflect-disabled, but if i manually change that to just disabled the view updates and angually looks disabled. Any idea how to make it work visually ? – Henrik Bøgelund Lavstsen Jul 05 '20 at 05:11
11

use [disabled] atttribute with button

<button class="example-20-width"  [disabled]="true" mat-raised-button disabled>Edit Client</button>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
6

Use [disabled] attribute

ts file

review_btn=true;

html file

  <button mat-raised-button [disabled]="review_btn" color="primary" mat-button (click)="reviewCreate()">Save</button>
Dinesh Kumar
  • 353
  • 7
  • 16
3

Another alternative is to use a template reference variable and get the information from the MatSelect:


<mat-form-field class="example-full-width" #selectedFood>
  <mat-select  placeholder="Select customer">
    <mat-option *ngFor="let food of books.data" 
                 [value]="food.company">
      {{food.company}}
    </mat-option>
    <mat-option >
    </mat-option>
  </mat-select>
</mat-form-field>
<button mat-stroked-button [disabled]="selectedFood.empty">
  Validate
</button>

See angular official documentation on template reference variables.

Yoann
  • 693
  • 5
  • 6
1

html file

<button mat-stroked-button color="primary" [disabled]="isNextButton" (click)="setSecurity()">Next</button>

ts file

isNextButton = true;

setSecurity() { this.isNextButton = false;}
0

If you assign an ngModel to the mat-select, you can then check to see if that model has a value:

                <mat-select  placeholder="Select customer" [(ngModel)]="book">
                  <mat-option *ngFor="let food of books.data" 
                      [value]="food.company">
                    {{food.company}}
                  </mat-option>
                  <mat-option >
                    </mat-option>
                </mat-select>

You can then check to see if a value has been selected in your button:

<button class="example-20-width" mat-raised-button [disabled]="!book">Edit Client</button>
Timmah
  • 1