Conditionally make input field readonly in Angular 2 or 4: Advice + Best/which way to do it

how to make input field not editable in angular 7
angular 6 readonly textarea
how to make select box readonly in angularjs
angular make form field read only
angular 2 readonly property
make mat form field not editable
how to set readonly attribute in angularjs
angular 8 ng readonly

I was attempting to answer someone elses question. And in doing so realised there was quite a bit of uncertainty in my mind about a few things. I'm hoping someone can provide feedback on the numbered points 1..4:

Task: Conditionally make input field readonly

Relevant section of HTML:

<input type="text" placeholder="Club Name" #clubName>

Add this to Typescript component.

// class properties
@ViewChild('clubName')
inp:HTMLInputElement; // Could also use interface Element

// conditionally set in some other methods of class
inp.setAttribute('readonly', 'readonly');
inp.removeAttribute('readonly');

Have to say this is a grey area for me.

  1. Is referencing HTMLInputElement or Element directly with @ViewChild in Angular 2+ a bad practice? Only, I've often seen examples using ElementRef or chaining off to nativeElement from ElementRef.

Since VS Studio doesn't have Intelli-sense for those, I suddenly feel like I'm coding in the dark. i.e, you never get feedback about methods setAttribute or removeAttribute, their parameter requirements etc. (I'm aware of As to cast too)


  1. Then, after looking at the docs, I suspect you can do this directly on your input in the HTML template:

<input [attr.readonly]= "isReadOnly">

IIRC I think you have to do this way with a property get in Typescript:

get isReadOnly() :boolean {
}

Is this way valid?


  1. I'm wondering, can you do the method syntax too in the HTML template:

<input [attr.readonly]= "isReadOnly()">

Typescript

isReadOnly() :boolean {
}

Is this way valid?


4. In summary, what's the best approach?

Update: There is also *ngIF so you output one of two input elements with same name. But that sounds to me like a sledgehammer to crack a nut.

You need to use the following (Angular 4):

<input [readonly]="isReadOnly">

If you use att.readonly then the input will always be read-only because the readonly attribute will be present even if its value is false. By using [readonly] Angular will only place the attribute if isReadOnly is true.

In HTML, the following is sufficient to cause an input to be read-only:

<input readonly>

Ng-readonly angular 6, Make div readonly , Conditionally make input field readonly in Angular 2 or 4: Advice + Best/which way to do it - angular. 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.

All depends on what you want to achieve. At first look, I would say that pct. 2 is the simplest way to do it:

<input [attr.readonly]= "isReadOnly">

Then, on your component you declare the variable:

isReadOnly: boolean;

After, you assign the value as you wish:

// when you want to be read-only
isReadOnly = true;
// whe you want to be editable
isReadOnly = false;

Angular ng-readonly Directive, Conditionally make input field readonly in Angular 2 or 4: Advice + Best/which way to do it. angular 7 readonly input angular read only form field how to make� Conditionally make input field readonly in Angular 2 or 4: Advice + Best/which way to do it I was attempting to answer someone elses question. And in doing so realised there was quite a bit of uncertainty in my mind about a few things.

None of them worked for me.. Finally found solution by creating custom directive angular.. Directives are powerful feature of angular

  1. Create Custom Directive

@Directive({
  selector: '[readonly],[readOnly]',
  host: {
    '[attr.readonly]': '_isReadonly ? "" : null'
  }
})
class ReadonlyDirective {
  _isReadonly = false;

  @Input() set readonly (v) {
     this._isReadonly = coerceBooleanProperty(v);
  };

  ngOnChanges(changes) {
    console.log(changes);
  }
}

Add readonly to FormControl formstate � Issue #11447 � angular , Task: Conditionally make input field readonly. Relevant section of HTML: <input type="text" placeholder="Club Name" #clubName>. Add this to Typescript� Stack Exchange network consists of 177 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

You can use <input readonly="{{ variable }}>".

In your *.component.ts, initialize the variable:

private variable: boolean = true;

angular-readonly-input, Conditionally make input field readonly in Angular 2 or 4: Advice + , You need In HTML, you cannot set the readonly attribute to false (the presence Overview. For my current employer: I have: recently started a conversion project, using Java 1.8+ gained Automation Anywhere V10 + V11 Advanced RPA Certification .Plus V11 Masters Certification.

Angular 4 Forms: Nesting and Input Validation, The ng-readonly directive sets the readonly attribute of a form field (input or In HTML, you cannot set the readonly attribute to false (the presence of the� Hi there thanks for provided the answer its worthful but I have multiple fields like Requestor as choice field on clicking the value "others" the Requestor_description need to show similarly I have another fields like Category, Assignee on clicking others as a choice Category_description, Assignee_description need to show. i have tried so many times using above procedure but cannot able to get

Angular 4 readonly form, kara added the hotlist: angular-core-team label on Mar 2, 2017. @kara The problem is, disabled fields are not existing in the form.value. improve support for readonly controls OR improve how to subscribe to form changes with disabled controls In my case i recommend*** to set fields to disable when:. Option 4, while still a bit ugly, is in my opinion the best option, because it’s just plain, valid CSS, but it uses a Microsoft-specific media query, with the effect that only IE will parse it. It doesn’t rely on IE conditional comments, so it works in IE10+.

Toggle light/dark theme. Toggle Zen Mode. Project. Download Project. Info. Starter project for Angular apps that exports to the Angular CLI. 7.7k. 32. Files. src . 31 Conditionally make input field readonly in Angular 2 or 4: Advice + Best/which way to do it 24 How to restore the menu bar in Visual Studio Code 18 What packages does 1) Java and 2) Groovy automatically import?

Comments
  • Define valid.... Because if you're asking if it works then test it and find out. This is a very broad question.... I use 2 & 3 on seperate occasions depending on what I need to compute to determine the bool.
  • This adds the readonly attribute to the input regardless of the value of variable.