Reputation: 78234
In the TS file 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 an input based on true
or false
.
I tried the following:
[disabled]="is_edit=='false' ? true : null"
[disabled]="is_edit=='true'"
[disabled]="is_edit"
Upvotes: 180
Views: 455274
Reputation: 577
If you are using reactive forms, do it on initialisation:
import { Component, Input, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: '...',
templateUrl: '...',
styleUrls: ['...'],
})
export class Component implements OnInit {
@Input() fc: FormControl<number> = new FormControl();
ngOnInit(): void {
this.disabled ? this.fc.disable() : this.fc.enable();
}
}
Upvotes: 0
Reputation: 988
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
Upvotes: 4
Reputation: 853
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>
Upvotes: 1
Reputation: 5391
<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;
}
Upvotes: 15
Reputation: 196
can use simply like
<input [(ngModel)]="model.name" disabled="disabled"
I got it like this way. so i prefer.
Upvotes: -2
Reputation: 3358
I prefer this solution
In HTML file:
<input [disabled]="dynamicVariable" id="name" type="text">
In TS file:
dynamicVariable = false; // true based on your condition
Upvotes: 4
Reputation: 457
And also if the input box/button has to remain disable, then simply
<button disabled>
or <input disabled>
works.
Upvotes: 0
Reputation: 475
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>
Upvotes: 2
Reputation: 759
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>
Upvotes: 61
Reputation: 4214
Try using attr.disabled
, instead of disabled
<input [attr.disabled]="disabled ? '' : null"/>
Upvotes: 417
Reputation: 2358
You could simply do this
<input [disabled]="true" id="name" type="text">
Upvotes: 37
Reputation: 119
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"/>
Upvotes: 3
Reputation: 391
What you are looking for is disabled="true". Here is an example:
<textarea class="customPayload" disabled="true" *ngIf="!showSpinner"></textarea>
Upvotes: 2
Reputation: 73337
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
Upvotes: 47
Reputation: 8202
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;
}
}
Upvotes: 2
Reputation: 32757
I presume you meant false
instead of 'false'
Also, [disabled]
is expecting a Boolean
. You should avoid returning a null
.
Upvotes: 7