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?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Deep
  • 123
  • 1
  • 3
  • 16
  • 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 – DetectivePikachu Nov 11 '19 at 14:53
  • Actually I don't know how to access the element in the html. How can I access that? – Deep Nov 11 '19 at 14:54

3 Answers3

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.

Zee
  • 483
  • 3
  • 10
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;
Santosh Shinde
  • 1,206
  • 10
  • 16
-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');