From d0dbc67cc0a3c407cb6459a5a95881647e9feaa6 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Thu, 6 Oct 2016 11:40:07 +0200 Subject: [PATCH] fix(calender, timeAgo): automatic update broken The updates are done outside the Angular zone, so Angular can't tell it the pipe needs to be updated. Fix by calling `markForCheck()` inside the NgZone. --- src/CalendarPipe.spec.ts | 3 +++ src/CalendarPipe.ts | 4 +++- src/TimeAgoPipe.spec.ts | 3 +++ src/TimeAgoPipe.ts | 6 ++++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/CalendarPipe.spec.ts b/src/CalendarPipe.spec.ts index 6da55df..cc84158 100644 --- a/src/CalendarPipe.spec.ts +++ b/src/CalendarPipe.spec.ts @@ -8,6 +8,9 @@ class NgZoneMock { runOutsideAngular (fn: Function) { return fn(); } + run(fn: Function) { + return fn(); + } }; describe('CalendarPipe', () => { diff --git a/src/CalendarPipe.ts b/src/CalendarPipe.ts index 6eb95b2..fed3a7b 100644 --- a/src/CalendarPipe.ts +++ b/src/CalendarPipe.ts @@ -30,7 +30,9 @@ export class CalendarPipe implements PipeTransform, OnDestroy { // values such as Today will need to be replaced with Yesterday after midnight, // so make sure we subscribe to an EventEmitter that we set up to emit at midnight this._ngZone.runOutsideAngular(() => - this._midnightSub = CalendarPipe._midnight.subscribe(() => this._cdRef.markForCheck())); + this._midnightSub = CalendarPipe._midnight.subscribe(() => { + this._ngZone.run(() => this._cdRef.markForCheck()); + })); } transform(value: Date | moment.Moment, ...args: any[]): any { diff --git a/src/TimeAgoPipe.spec.ts b/src/TimeAgoPipe.spec.ts index fac8377..312b296 100644 --- a/src/TimeAgoPipe.spec.ts +++ b/src/TimeAgoPipe.spec.ts @@ -7,6 +7,9 @@ class NgZoneMock { runOutsideAngular (fn: Function) { return fn(); } + run(fn: Function) { + return fn(); + } }; describe('TimeAgoPipe', () => { diff --git a/src/TimeAgoPipe.ts b/src/TimeAgoPipe.ts index ff9cfdf..5079497 100644 --- a/src/TimeAgoPipe.ts +++ b/src/TimeAgoPipe.ts @@ -1,6 +1,6 @@ /* angular2-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ -import {Pipe, ChangeDetectorRef, PipeTransform, OnDestroy, NgZone} from '@angular/core'; +import { Pipe, ChangeDetectorRef, PipeTransform, OnDestroy, NgZone } from '@angular/core'; import * as moment from 'moment'; // under systemjs, moment is actually exported as the default export, so we account for that @@ -19,7 +19,9 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { const timeToUpdate = this._getSecondsUntilUpdate(momentInstance) * 1000; this._currentTimer = this._ngZone.runOutsideAngular(() => { if (typeof window !== 'undefined') { - return window.setTimeout(() => this._cdRef.markForCheck(), timeToUpdate); + return window.setTimeout(() => { + this._ngZone.run(() => this._cdRef.markForCheck()); + }, timeToUpdate); } }); return momentConstructor(value).from(momentConstructor(), omitSuffix);