Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 ternary if to add disabled to an input [duplicate]

How may I add disabled to an input if a condition is met?

What I have today: <input class="previous" [attr.disabled] = "active === 1 ? 'disabled' : ''">

But this adds disabled="disabled" and I want only disabled.

So what I need is: <input class="previous" disabled> if the condition is met.

like image 520
William Avatar asked Jan 16 '17 15:01

William


People also ask

How to disable input field in Angular based on condition?

The ng-disabled directive sets the disabled attribute of a form field (input, select, or textarea). The form field will be disabled if the expression inside the ng-disabled attribute returns true. The ng-disabled directive is necessary to be able to shift the value between true and false .

How to disable button on some condition in Angular?

the button will be disabled when valid is false and the angular formGroup , SAForm is not valid. A recommendation here as well, Please make the button of type button not a submit because this may cause the whole form to submit and you would need to use invalidate and listen to (ngSubmit) .

What is ATTR disabled Angular?

To disable elements just use attribute disabled rather than true or false. To enable it again, you need to remove the disabled attribute. In your code [attr. disabled] is setting the value to true or false, what you need is just use [disabled] instead of [attr.

Can't bind to readOnly since it isn't a known property of select?

Can't bind to 'readOnly' since it isn't a known property of 'ng-select'. If 'ng-select' is an Angular component and it has 'readOnly' input, then verify that it is part of this module. If 'ng-select' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.


1 Answers

An attribute can be removed by passing the value null

[attr.disabled] = "active === 1 ? 'disabled' : null">
like image 177
Günter Zöchbauer Avatar answered Sep 20 '22 05:09

Günter Zöchbauer