Skip to content

Commit 18870fc

Browse files
authored
Merge pull request #60 from cat-mandres/fix-on-push
Manually trigger change detection after loading an image to support ChangeDetectionStrategy.OnPush
2 parents bd52c3c + 85856aa commit 18870fc

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/projects/ngx-image-gallery/src/lib/components/ngx-image-gallery/ngx-image-gallery.component.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
Output,
1111
OnChanges,
1212
SimpleChanges,
13-
ViewChild
13+
ViewChild,
14+
ChangeDetectorRef
1415
} from '@angular/core';
1516

1617
import {assign, debounce} from 'lodash';
@@ -181,14 +182,16 @@ export class NgxImageGalleryComponent implements OnInit, OnChanges {
181182
this.loadImage(imageIndex)
182183
.then(_imageIndex => {
183184
this.activeImageIndex = _imageIndex;
185+
// Trigger change detection manually to support ChangeDetectionStrategy.OnPush
186+
this.cdRef.detectChanges();
184187

185188
// scroll thumbnails
186189
setTimeout(() => {
187190
this.fitThumbnails();
188191
setTimeout(() => this.scrollThumbnails(), 300);
189192
});
190193
})
191-
.catch(error => {
194+
.catch(error => {
192195
console.warn(error)
193196
this.onError.next(error);
194197
});
@@ -223,7 +226,8 @@ export class NgxImageGalleryComponent implements OnInit, OnChanges {
223226
constructor(
224227
public sanitizer: DomSanitizer,
225228
private galleryElem: ElementRef,
226-
private renderer: Renderer2
229+
private renderer: Renderer2,
230+
private cdRef: ChangeDetectorRef
227231
) {}
228232

229233
ngOnInit() {
@@ -265,7 +269,7 @@ export class NgxImageGalleryComponent implements OnInit, OnChanges {
265269
}
266270

267271
}
268-
272+
269273
// keyboard event
270274
@HostListener('window:keydown', ['$event'])
271275
public onKeyboardInput(event: KeyboardEvent) {

0 commit comments

Comments
 (0)