Disable the entire form at once (Angular reactive form)
Asked Answered
B

2

44

Is there any way to disable the entire form in angular when using Reactive forms. I know it is possible to make them disable one by one.

 this.tempForm = this.fb.group({
  m26_type:  '',
  m26_name:  ''
 })
this.tempForm.get('m26_type').disable();

Is it possible to disable the whole form rather than make every controller disable separately?

Borodino answered 24/8, 2017 at 6:1 Comment(1)
Have a look at AbstractControl.disableDoggy
L
58
this.tempForm.disable();

Disables the control. This means the control will be exempt from validation checks and excluded from the aggregate value of any parent. Its status is DISABLED.

If the control has children, all children will be disabled to maintain the model.

LINK

UPDATE

Plunker link - https://plnkr.co/edit/CFC4uKpvfE4otJ2PWdkc?p=preview

Linhliniment answered 24/8, 2017 at 6:14 Comment(4)
@Freak001 updated the code with a plunker link check thatLinhliniment
@Freak001 jope it helped ?Linhliniment
Yeah it helped. In my case problem was I am using custom form controls in reactive forms. When using custom form controls i reactive forms need to implement setDisabledState(). angular.io/api/forms/ControlValueAccessorBorodino
Remember for template driven forms the code is ngFormName.form.disable() or enable(). Also they are functions as the properties 'disabled' and 'enabled' are read-only.Slambang
M
1

If you use the template driven (with ngModel) you can add to your code also

<form #myForm="ngForm">
//at ts
//create view child
@viewChild('myForm')form:any;
//and in your function of reset
this.form.disable()
Marylouisemaryly answered 27/4, 2022 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.