0

This is my first angular project. Just started today. I have a form and I want to disable the input field.

This is my .html file below:

<div class="form-group">
<label for="projectTitle" class="label">Project Title</label>
    <input type="text" fullWidth id="projectTitle" value= 
    {{projectDetails.name}}" placeholder="Project Title">
</div>

How to disable the input from the .ts file?

2
  • 1
    I think he wants to know how to disable it from the typescript file, not only the html. In any case, you just bind disabled to a boolean in your ts file and if you want it to be disabled, you just set the boolean that way Commented Nov 11, 2019 at 14:53
  • Actually I don't know how to access the element in the html. How can I access that? Commented Nov 11, 2019 at 14:54

3 Answers 3

10

In HTML add conditional [disabled] attribute

<div class="form-group">
<label for="projectTitle" class="label">Project Title</label>
    <input type="text" fullWidth id="projectTitle" [value]="projectDetails.name" [disabled]="isEnabled" placeholder="Project Title">
</div>

Toggle it to true or false from .ts

this.isEnabled = false;

If it helped, consider upvoting to the answer, please.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mohd Zeeshan. It helped
0

Template

<div class="form-group">
    <label for="projectTitle" class="label">Project Title</label>
        <input type="text" [attr.disabled]="isEnabled? '' : null" fullWidth id="projectTitle" value= 
        {{projectDetails.name}}" placeholder="Project Title">
    </div>

try component property instead of true to disable conditionally.

.ts
isEnabled:boolean = false;

1 Comment

And in the .ts file how to change it?
-2

just add disabled tag to your input:

<input type="text" fullWidth id="projectTitle" value= 
    {{projectDetails.name}}" disabled placeholder="Project Title">

in jquery : disable:

$("#projectTitle").attr('disabled', 'disabled');

enable:

$("#projectTitle").removeAttr('disabled');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.