How to disable a input in angular2
Try using attr.disabled
, instead of disabled
<input [attr.disabled]="disabled ? '' : null"/>
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
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>