How to make readonly all inputs in some div in Angular2?

AngularTypescriptReadonlyAngular2 Forms

Angular Problem Overview


I have a page with many inputs, and I want to make it 'readOnly' I find this solution: https://stackoverflow.com/questions/35212475/how-to-change-html-element-readonly-and-required-attribute-in-angular2-typescrip

But I don't want to do it for every input separately.

How can I add readOnly property to all inputs in some div.

Angular Solutions


Solution 1 - Angular

Try this in input field:

[readonly]="true"

Hope, this will work.

Solution 2 - Angular

If you want to do a whole group, not just one field at a time, you can use the HTML5 <fieldset> tag.

<fieldset [disabled]="killfields ? 'disabled' : null">

    <!-- fields go here -->

</fieldset>

Solution 3 - Angular

If using reactive forms, you can also disable the entire form or any sub-set of controls in a FormGroup with myFormGroup.disable().

Solution 4 - Angular

If you meant disable all the inputs in an Angular form at once:

1- Reactive Forms:

myFormGroup.disable() // where myFormGroup is a FormGroup 

2- Template driven forms (NgForm):

You should get hold of the NgForm in a NgForm variable (for ex. named myNgForm) then

myNgForm.form.disable() // where form here is an attribute of NgForm 
                      // & of type FormGroup so it accepts the disable() function

In case of NgForm , take care to call the disable method in the right time

To determine when to call it, You can find more details in this Stackoverflow answer

Solution 5 - Angular

All inputs should be replaced with custom directive that reads a single global variable to toggle readonly status.

// template
<your-input [readonly]="!childmessage"></your-input>

// component value
childmessage = false;

Solution 6 - Angular

Just set css property of container div 'pointer-events' as none i.e. 'pointer-events:none;'

Solution 7 - Angular

You can do like this. Open a ts file ad there make an interface with inputs you want and in the page you want to show under export class write

readonly yourinterface = yourinterface
readonly level: number[] = [];

and in your template do like this *ngFor="let yourtype of yourinterface"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn DoeView Question on Stackoverflow
Solution 1 - AngularAvnesh ShakyaView Answer on Stackoverflow
Solution 2 - Angularuser5868538View Answer on Stackoverflow
Solution 3 - AngularsnortView Answer on Stackoverflow
Solution 4 - AngularAhmed ElkoussyView Answer on Stackoverflow
Solution 5 - AngularLeo LaneseView Answer on Stackoverflow
Solution 6 - AngularRavinder KumarView Answer on Stackoverflow
Solution 7 - Angularuser8956519View Answer on Stackoverflow