Skip to content

Commit

Permalink
Fixed #7939 - Calendar is not working correctly in overlayPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Jul 12, 2019
1 parent fb54f98 commit f4c6ca1
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/app/components/calendar/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {NgModule,Component,ElementRef,OnDestroy,OnInit,Input,Output,SimpleChange,EventEmitter,forwardRef,Renderer2,
ViewChild,ChangeDetectorRef,TemplateRef,ContentChildren,QueryList} from '@angular/core';
ViewChild,ChangeDetectorRef,TemplateRef,ContentChildren,QueryList, NgZone} from '@angular/core';
import {trigger,state,style,transition,animate,AnimationEvent} from '@angular/animations';
import {CommonModule} from '@angular/common';
import {ButtonModule} from '../button/button';
Expand Down Expand Up @@ -527,7 +527,7 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {
}
}

constructor(public el: ElementRef, public renderer: Renderer2, public cd: ChangeDetectorRef) {}
constructor(public el: ElementRef, public renderer: Renderer2, public cd: ChangeDetectorRef, private zone: NgZone) {}

ngOnInit() {
const date = this.defaultDate||new Date();
Expand Down Expand Up @@ -2052,12 +2052,16 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {

bindDocumentClickListener() {
if (!this.documentClickListener) {
this.documentClickListener = this.renderer.listen('document', 'click', (event) => {
if (this.isOutsideClicked(event) && this.overlayVisible) {
this.hideOverlay();
}

this.cd.detectChanges();
this.zone.runOutsideAngular(() => {
this.documentClickListener = this.renderer.listen('document', 'click', (event) => {
if (this.isOutsideClicked(event) && this.overlayVisible) {
this.zone.run(() => {
this.hideOverlay();
});
}

this.cd.markForCheck();
});
});
}
}
Expand Down

2 comments on commit f4c6ca1

@Rillist
Copy link

@Rillist Rillist commented on f4c6ca1 Sep 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting an error from this change, 'Cannot read property 'runOutsideAngular' of undefined'.

This only happens in AOT, or production build. So I cannot reproduce on stackblitz. :(

Hopefully I can figure something out... Don't understand why the zone is undefined.

@Rillist
Copy link

@Rillist Rillist commented on f4c6ca1 Sep 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a SO post about zone being undefined and the fix was to explicitly inject NgZone.
https://stackoverflow.com/questions/46536430/prerendering-failed-because-of-ngzone-reference-error

Seems to work for this issue too. If I have some extra time to remember how to contribute, I will try to make a PR with the fix.

But if I forget or someone needs it before then...
Change private zone: NgZone to @Inject(NgZone) private zone: NgZone inside calendar.ts

You will also need to import Inject from @angular/core

image

Please sign in to comment.