From cc91db344ac74dc9e5e9d0ed04ec9be1e4d81a79 Mon Sep 17 00:00:00 2001 From: Alex Malkevich Date: Thu, 19 Apr 2018 11:42:38 +0200 Subject: [PATCH] fix(directive): Check if inputs really changed when angular triggers change detection on them Because object structure might not change while the reference will closes #111 --- src/dynamic/dynamic.directive.spec.ts | 6 ++++++ src/dynamic/dynamic.directive.ts | 8 ++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/dynamic/dynamic.directive.spec.ts b/src/dynamic/dynamic.directive.spec.ts index 3447f07ff..6443fc0a0 100644 --- a/src/dynamic/dynamic.directive.spec.ts +++ b/src/dynamic/dynamic.directive.spec.ts @@ -138,6 +138,12 @@ describe('Directive: Dynamic', () => { fixture.componentInstance['inputs'] = null; expect(() => fixture.detectChanges()).not.toThrow(); }); + + it('should NOT throw exception when same inputs are reassigned with new object', () => { + fixture.detectChanges(); + fixture.componentInstance['inputs'] = { ...fixture.componentInstance['inputs'] }; + expect(() => fixture.detectChanges()).not.toThrow(); + }); }); describe('inputs with `NgComponentOutlet`', () => { diff --git a/src/dynamic/dynamic.directive.ts b/src/dynamic/dynamic.directive.ts index e70b6741b..ef0b963d8 100644 --- a/src/dynamic/dynamic.directive.ts +++ b/src/dynamic/dynamic.directive.ts @@ -89,8 +89,12 @@ export class DynamicDirective implements OnChanges, DoCheck, OnDestroy { this.bindOutputs(); } else { if (this._inputsChanged(changes)) { - this._updateInputChanges(this._getInputsChanges(this._inputs)); - this.updateInputs(!this._lastInputChanges); + const inputsChanges = this._getInputsChanges(this._inputs); + + if (inputsChanges) { + this._updateInputChanges(inputsChanges); + this.updateInputs(!this._lastInputChanges); + } } if (this._outputsChanged(changes)) {