Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable button if input length less than 3

I want to disable button if input length is less than 3:

 <input type="text" [(ngModel)]="newListItem">
 <button [disabled]="{ disabled : newListItem.length < 3 }"></button>

But this code doesn't work. How can I achieve my goal?

like image 973
Vnuuk Avatar asked Dec 23 '16 14:12

Vnuuk


People also ask

How do you conditionally disable a button?

To conditionally disable a button element in Vue. js, you can dynamically bind the disable attribute to an expression that evaluates to boolean true (to disable the button) or false (to enable the button).

Can buttons 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 you make a button unusable?

You can disable the <button> element in HTML by adding the disabled attribute to the element. The disabled attribute is a boolean attribute that allows you to disable an element, making the element unusable from the browser.


1 Answers

You only need to pass an expression to disabled property, there's no need to create object:

<button [disabled]="newListItem.length < 3"></button>
like image 54
Stefan Svrkota Avatar answered Sep 24 '22 18:09

Stefan Svrkota