177

In ts is_edit = true to disable...

<input [disabled]="is_edit=='false' ? true : null" id="name" type="text" [(ngModel)]="model.name" formControlName="name" class="form-control" minlength="2">

I just simply want to disable a input based on true or false.

I tried following:

[disabled]="is_edit=='false' ? true : null"
[disabled]="is_edit=='true'"
[disabled]="is_edit"
c69
  • 19,951
  • 7
  • 52
  • 82
Tampa
  • 75,446
  • 119
  • 278
  • 425
  • 10
    You already asked this question, and already had an answer, but then deleted the question. Again, open your browser console to notice the errors, fix them (i.e. use name and not formControlName), and then, it works: http://plnkr.co/edit/al2IkGkaZKpdLKKTfvKh?p=preview – JB Nizet Feb 11 '17 at 17:52
  • The real problem here is that you're mixing template-based forms with reactive forms. Use one or the other, not both. @AJT_82 has the correct answer. – adam0101 Dec 03 '18 at 17:05

15 Answers15

411

Try using attr.disabled, instead of disabled

<input [attr.disabled]="disabled ? '' : null"/>
Luca Ritossa
  • 1,118
  • 11
  • 22
fedtuck
  • 4,154
  • 3
  • 13
  • 3
  • 5
    Setting attr value to null in order to NOT add the attr to element - looks like a hack - but it actually works, and even makes sence, in a hindsight. – c69 Nov 29 '17 at 05:13
  • 2
    `disabled` equals to `true` or `false` – Belter Dec 18 '17 at 09:27
  • 26
    Thank you for an answer that actually works! So why does `attr.disabled` work when `disabled` does not? – Dylanthepiguy Jul 29 '18 at 00:27
  • Why angular document didn't say anything about this? – Ben Oct 31 '18 at 02:36
  • 5
    While this works, it's a hack that doesn't address the real problem of mixing template-based forms with reactive forms. The OP should be using one or the other, not both. – adam0101 Dec 03 '18 at 17:08
  • 5
    I think work also – Francesco Dec 05 '18 at 10:10
  • 2
    This does not work in more recent versions of Angular. You can use `[disabled]="disabled"`. – Edu Jan 09 '20 at 16:16
  • Importante note: [attr.disabled]=false won't work, it has to be null. Don't ask me why – David Glass Aug 20 '20 at 16:11
  • this is not a good approach, because the user can delete this attribute via the browser, in which case it will change in the data in the model? – Caner Dec 17 '21 at 17:16
55

If you want input to disable on some statement. use [readonly]=true or false instead of disabled.

<input [readonly]="this.isEditable" 
    type="text" 
    formControlName="reporteeName" 
    class="form-control" 
    placeholder="Enter Name" required>
nircraft
  • 8,242
  • 5
  • 30
  • 46
Usman Saleh
  • 699
  • 5
  • 8
45

I think I figured out the problem, this input field is part of a reactive form (?), since you have included formControlName. This means that what you are trying to do by disabling the input field with is_edit is not working, e.g your attempt [disabled]="is_edit", which would in other cases work. With your form you need to do something like this:

toggle() {
  let control = this.myForm.get('name')
  control.disabled ? control.enable() : control.disable();
}

and lose the is_edit altogether.

if you want the input field to be disabled as default, you need to set the form control as:

name: [{value: '', disabled:true}]

Here's a plunker

AT82
  • 71,416
  • 24
  • 140
  • 167
37

You could simply do this

<input [disabled]="true" id="name" type="text">
Ifesinachi Bryan
  • 2,240
  • 1
  • 19
  • 20
  • 1
    While this works, it suggests that "false" would enable the control, which it doesn´t since the presence of the disabled attribute is what diabled the control, not the value. – Mecaveli Oct 18 '18 at 11:17
  • @Mecaveli, setting [disabled] to false actually enables the control. makes the input field enabled. You could check over your angular application. – Ifesinachi Bryan Oct 18 '18 at 14:02
  • 1
    Just to correct myself here: As i learned, [disabled] doesn´t actually control the "disabled" html attribute value like [attr.disabled] would. Therefor, [disabled]="false" works although "false" as value for the html attribute "disable" would not. – Mecaveli Nov 12 '18 at 14:50
  • 6
    So in all, you should actually use your editor and try it out before bringing up a counter point. – Ifesinachi Bryan Nov 12 '18 at 16:18
  • Indeed, sorry for downvoting unfortunately can´t undo it now. Was misusing [attr.disabled] for years thinking (or rather not much thinking) it´s equal to [disabled]... – Mecaveli Nov 13 '18 at 20:22
  • 1
    This needs more context. – Ojonugwa Jude Ochalifu Aug 12 '21 at 11:44
14
<input [disabled]="is_edit" id="name" type="text">
<button (click)="is_edit = !is_edit">Change input state</button>

export class AppComponent {
  is_edit : boolean = false;
}
Yoav Schniederman
  • 5,253
  • 3
  • 28
  • 32
  • 1
    If you put console log in the isDisabled method, there are hundreds of console log are executed. If there is a complex logic in the isDisabled method, it might be a problem. So I am not sure this is a good solution. – Téwa Dec 21 '18 at 11:00
  • 1
    Do not put dynamic checking into Angular variables or you get an infinite loop. You can use `[disabled]="is_edit"` directly. Why the `isDisabled()` function? – alex351 Apr 25 '19 at 06:02
7

I presume you meant false instead of 'false'

Also, [disabled] is expecting a Boolean. You should avoid returning a null.

zurfyx
  • 31,043
  • 20
  • 111
  • 145
4

I prefer this solution

In HTML file:

<input [disabled]="dynamicVariable" id="name" type="text">

In TS file:

dynamicVariable = false; // true based on your condition 
Anirudh
  • 2,767
  • 5
  • 69
  • 119
3

A note in response to Belter's comment on fedtuck's accepted answer above, since I lack the reputation to add comments.

This is not true for any of which I am aware, in line with the Mozilla docs

disabled equals to true or false

When the disabled attribute is present the element is disabled regardless of value. See this example

<input placeholder="i can be changed"/>
<input disabled placeholder="i can NOT be changed"/>
<input disabled="true" placeholder="i can NOT be changed"/>
<input disabled="false" placeholder="i can NOT be changed"/>
severin
  • 99
  • 1
  • 4
  • 3
    that is true and wholly true for pure Html, but for angular, you have to provide a boolean value for disabled especially when you are doing two way binding. – Ifesinachi Bryan Mar 08 '18 at 09:10
3

Actually, the currently recommended approach when using reactive forms (in order to avoid 'changed after checked' errors) is not to use the disabled attribute with a reactive form directive. You should set up disabled property of this control in your component class and the disabled attribute will actually be set in the DOM for you.

<div>
      <form [formGroup]="form">
    <p>
        <input matInput type="text" placeholder="Name" formControlName="name"/>
    </p>
    </form>
</div>

and the component code:

form: FormGroup;
  public is_edit: boolean = true;
  constructor(
    private fb: FormBuilder
    ) {
    
  }

  ngOnInit() {
    this.form = this.fb.group({
      name: [{value: '', disabled: !this.is_edit}, [Validators.required, Validators.minLength(2)]],
    });
  }

Here is a plunker with the working code: http://plnkr.co/edit/SQjxKBfvvUk2uAIb?preview

Sinisa Rudan
  • 561
  • 8
  • 10
2

Demo

make is_edit of type boolean.

<input [disabled]=is_edit id="name" type="text">

export class App {
  name:string;
  is_edit: boolean; 
  constructor() {
    this.name = 'Angular2'
    this.is_edit = true;
  }
}
Ajey
  • 7,924
  • 12
  • 62
  • 86
2

What you are looking for is disabled="true". Here is an example:

<textarea class="customPayload" disabled="true" *ngIf="!showSpinner"></textarea>
Sumeet
  • 391
  • 3
  • 4
  • 6
    This is incorrect, the HTML and DOM `disabled` attribute is a "boolean attribute" which means only the attribute name needs to be specified - or its value must equal `disabled`, e.g. `disabled="disabled" - it does not recognize the values of `true` or `false` - so `disabled="false"` will still cause the element to be disabled. – Dai May 20 '18 at 01:50
2

Disable TextBox in Angular 7

<div class="center-content tp-spce-hdr">
  <div class="container">
    <div class="row mx-0 mt-4">
      <div class="col-12" style="padding-right: 700px;" >
          <div class="form-group">
              <label>Email</label>
                <input [disabled]="true" type="text" id="email" name="email" 
                [(ngModel)]="email" class="form-control">
          </div>
     </div>
   </div>
 </div>

nick
  • 425
  • 4
  • 12
1

Disabled Select in angular 9.

one thing keep in mind disabled work with boolean values in this example, I am using the (change) event with the select option if the country is not selected region will be disabled.

find.component.ts file

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-find',
  templateUrl: './find.component.html',
  styleUrls: ['./find.component.css']
})
export class FindComponent implements OnInit {
  isCountrySelected:boolean;

  constructor() { }
  //onchange event disabled false
 onChangeCountry(id:number){
   this.isCountrySelected = false;
 }
  ngOnInit(): void {
    //initially disabled true
    this.isCountrySelected = true;
  }

}

find.component.html

//Country select option
<select  class="form-control"  (change)="onChangeCountry()" value="Choose Country">
                    <option value="">Choose a Country</option>
                    <option value="US">United States</option>
                     
</select>
//region disabled till country is not selected 
<select class="form-control" [disabled]="isCountrySelected">
                    <option value="">Choose a Region</option>
                    <option value="">Any regions.</option>
                    
                </select>
Saad Abbasi
  • 745
  • 11
  • 25
0

And also if the input box/button has to remain disable, then simply <button disabled> or <input disabled> works.

ascripter
  • 5,665
  • 12
  • 45
  • 68
Priyanka Arora
  • 409
  • 4
  • 10
-2

can use simply like

     <input [(ngModel)]="model.name" disabled="disabled"

I got it like this way. so i prefer.

Brock James
  • 196
  • 1
  • 11