Skip to content

Commit bbd60c4

Browse files
committed
fix(element): fix change detection for OnPush components
1 parent df563ab commit bbd60c4

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

projects/elements/src/lib/lazy-elements/lazy-element-dynamic/lazy-element-dynamic.directive.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
OnInit,
55
TemplateRef,
66
ViewContainerRef,
7-
ComponentFactoryResolver
7+
ComponentFactoryResolver,
8+
ChangeDetectorRef
89
} from '@angular/core';
910

1011
import {
@@ -32,7 +33,8 @@ export class LazyElementDynamicDirective implements OnInit {
3233
private vcr: ViewContainerRef,
3334
private template: TemplateRef<any>,
3435
private elementsLoaderService: LazyElementsLoaderService,
35-
private cfr: ComponentFactoryResolver
36+
private cfr: ComponentFactoryResolver,
37+
private cdr: ChangeDetectorRef
3638
) {}
3739

3840
ngOnInit() {
@@ -70,16 +72,19 @@ export class LazyElementDynamicDirective implements OnInit {
7072
}
7173
this.vcr.clear();
7274
this.vcr.createEmbeddedView(this.template);
75+
this.cdr.markForCheck();
7376
})
7477
.catch(error => {
7578
const errorComponent =
7679
elementConfig.errorComponent || options.errorComponent;
7780
this.vcr.clear();
7881
if (this.errorTemplateRef) {
7982
this.vcr.createEmbeddedView(this.errorTemplateRef);
83+
this.cdr.markForCheck();
8084
} else if (errorComponent) {
8185
const factory = this.cfr.resolveComponentFactory(errorComponent);
8286
this.vcr.createComponent(factory);
87+
this.cdr.markForCheck();
8388
} else {
8489
console.error(
8590
`${LOG_PREFIX} - Loading of element <${this.tag}> failed, please provide <ng-template #error>Loading failed...</ng-template> and reference it in *axLazyElementDynamic="errorTemplate: error" to display customized error message in place of element\n\n`,

projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
OnInit,
55
TemplateRef,
66
ViewContainerRef,
7-
ComponentFactoryResolver
7+
ComponentFactoryResolver,
8+
ChangeDetectorRef
89
} from '@angular/core';
910

1011
import {
@@ -27,7 +28,8 @@ export class LazyElementDirective implements OnInit {
2728
private vcr: ViewContainerRef,
2829
private template: TemplateRef<any>,
2930
private elementsLoaderService: LazyElementsLoaderService,
30-
private cfr: ComponentFactoryResolver
31+
private cfr: ComponentFactoryResolver,
32+
private cdr: ChangeDetectorRef
3133
) {}
3234

3335
ngOnInit() {
@@ -54,16 +56,19 @@ export class LazyElementDirective implements OnInit {
5456
.then(() => {
5557
this.vcr.clear();
5658
this.vcr.createEmbeddedView(this.template);
59+
this.cdr.markForCheck();
5760
})
5861
.catch(() => {
5962
this.vcr.clear();
6063
const errorComponent =
6164
elementConfig.errorComponent || options.errorComponent;
6265
if (this.errorTemplateRef) {
6366
this.vcr.createEmbeddedView(this.errorTemplateRef);
67+
this.cdr.markForCheck();
6468
} else if (errorComponent) {
6569
const factory = this.cfr.resolveComponentFactory(errorComponent);
6670
this.vcr.createComponent(factory);
71+
this.cdr.markForCheck();
6772
} else {
6873
console.error(
6974
`${LOG_PREFIX} - Loading of element <${elementTag}> failed, please provide <ng-template #error>Loading failed...</ng-template> and reference it in *axLazyElement="errorTemplate: error" to display customized error message in place of element`

0 commit comments

Comments
 (0)