我创建了一个简单的秒表管道,该管道应显示秒差。在某个日期之后:
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
name: 'stopwatch',
pure: false
})
export class StopwatchPipe implements PipeTransform {
transform(value: Date, args?: any): any {
return moment().diff(value);
}
}
我使管道不纯,无法永久检测到更改。我也找到了该线程,但是不确定如何将更改检测集成到管道中: How to manage Angular2 "expression has changed after it was checked" exception when a component property depends on current datetime
错误:ExpressionChangedAfterItHasBeenCheckedError:检查表达式后,表达式已更改。先前的值:'null:672162'。当前值:“空:672163”。
问题:如何在没有上述错误的情况下正确返回时间差?