Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { AbstractControl } from '@angular/forms';
- type ControlChangeOptions = {
- onlySelf?: boolean;
- emitEvent?: boolean;
- };
- export class FormUtils {
- static readonly defaultOptions: ControlChangeOptions = {
- onlySelf: false,
- emitEvent: true,
- };
- /**
- * Updates every value of given control array
- */
- static patchControls<T>(
- controls: AbstractControl[],
- value: T,
- options = FormUtils.defaultOptions
- ): void {
- if (!Array.isArray(controls)) {
- return;
- }
- controls.forEach(control => control.patchValue(value, options));
- }
- /**
- * Disables each control in the array
- */
- static disableControls(
- controls: AbstractControl[],
- options = FormUtils.defaultOptions
- ): void {
- if (!Array.isArray(controls)) {
- return;
- }
- controls.forEach(control => control.disable(options));
- }
- /**
- * Enables each control in the array
- */
- static enableControls(
- controls: AbstractControl[],
- options = FormUtils.defaultOptions
- ): void {
- if (!Array.isArray(controls)) {
- return;
- }
- controls.forEach(control => control.enable(options));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement