From 2349baa542e7e1b77851dfda8030b15212fa69e4 Mon Sep 17 00:00:00 2001 From: mostwired Date: Mon, 31 Jul 2017 22:18:55 -0400 Subject: [PATCH] Update dynamic-form.component.ts Change detection throws errors: Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '{}'. Current value: '{ "name": "Todd" }'. I am using setTimeout to trigger the next CD cycle. --- .../dynamic-form/dynamic-form.component.ts | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/app/dynamic-form/containers/dynamic-form/dynamic-form.component.ts b/app/dynamic-form/containers/dynamic-form/dynamic-form.component.ts index 84e9913..26ff4b0 100644 --- a/app/dynamic-form/containers/dynamic-form/dynamic-form.component.ts +++ b/app/dynamic-form/containers/dynamic-form/dynamic-form.component.ts @@ -78,21 +78,25 @@ export class DynamicFormComponent implements OnChanges, OnInit { } setDisabled(name: string, disable: boolean) { - if (this.form.controls[name]) { - const method = disable ? 'disable': 'enable'; - this.form.controls[name][method](); - return; - } - - this.config = this.config.map((item) => { - if (item.name === name) { - item.disabled = disable; + setTimeout(() => { + if (this.form.controls[name]) { + const method = disable ? 'disable': 'enable'; + this.form.controls[name][method](); + return; } - return item; - }); + + this.config = this.config.map((item) => { + if (item.name === name) { + item.disabled = disable; + } + return item; + }); + }, 0); } setValue(name: string, value: any) { - this.form.controls[name].setValue(value, {emitEvent: true}); + setTimeout(() => { + this.form.controls[name].setValue(value, {emitEvent: true}); + }, 0); } }