0

I am using Angular material button to create a common button component to be used in my Angular 13 application. When I use the button component, the 'disabled' property does not work. I mean the button looks greyed out but its still clickable. Here is my code, am I missing anything?

COMMON COMPONENT:

<button mat-button [disabled]="disabled" (click)="clickViewDetails(label)"><mat-icon *ngIf="icon">{{ icon }}</mat-icon> {{ label }}</button>

@Input() disabled:boolean = true;

TEMPLATE:

This does not work: <my-button (click)="signIn()" [disabled]="signBtn_disabled"> </my-button>

This works : <button mat-button [disabled]="signBtn_disabled" (click)="signIn()">Sign In</button>

1 Answer 1

2

The (click) is "attached" to you component, not to the mat-button. It's the reason you can "click" your component. You can use some like

<my-button (click)="!signBtn_disabled && signIn()" 
      [disabled]="signBtn_disabled">

Yes, is very common use the construction (event)="condition && function()". If the condition is not fullfilled, the function is not executed

Other option is that your component has an Output

@Output() customClick:eventEmitter=new EventEmitter<any>()

<mat-button (click)="customClick.emit(true)" [disabled]=...>

And you use

<my-button (customClick)="signIn()"></my-button>

Update I'm asking me if we can call to our "Output" click.

Saddly the click happens always we make a "click" in our component (not only in the inner button). So Imagine we have in out my-button component some like

  <h1>I'm a custom button</h1>
  <button (click)="onClick($event)" [disabled]="...">button</button>`

  //we call out Output "click"

  @Output()click:EventEmitter<any>=new EventEmitter<any>()
  onClick(event)
  {
    event.stopPropagation()
    this.click.next(true)
  }

We can use in out parent

<my-button (click)="click($event)"></my-button>

All look like good if we "click" in the button, but if we click in the "h1", the event click happens in our parent. The only is check the event in parent

 click($event){
   if ($event===true)
   {
       ..do something..
   }
 }

Or in parent

 <my-button (click)="$event===true && click($event)"></my-button>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that worked !!!!! But can I do some changes in my custom component so that I don't have to change it everywhere else?
I update the answer to add the case you call to the "Output" click but sadly, if your "my-button" is some more than a button, the "click" event happens always :(

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.