Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a button with an observable?

Tags:

angular

I have a next button that should be disabled if my observable value is false.

The observable is dataSource.next (if there is more data - next is true, otherwise it's false). This disables the button and stops the user from navigating if there is no more data.

component.html:

<button mat-button (click)="next()" [disabled]="!dataSource.next$ | async">
    <mat-icon>navigate_next</mat-icon>
</button>

However, although the value when outputted for dataSource.next is correct, it does not work as intended, perhaps it's because I'm trying to disable it if the value is false rather than true?

For example, when there is no more data, dataSource.next is false but the button is still enabled.

What is the solution?

like image 920
Kay Avatar asked Mar 26 '18 09:03

Kay


People also ask

Can we disable button in HTML?

The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).

How do I disable a button in angular?

Button component can be enabled/disabled by giving disabled property. To disable Button component, the disabled property can be set as true .


1 Answers

Put the async pipe in parenthesis.

<button mat-button (click)="next()" [disabled]="!(dataSource.next$ | async)">
  <mat-icon>navigate_next</mat-icon>
</button>
like image 118
Tomasz Kula Avatar answered Sep 19 '22 11:09

Tomasz Kula