【发布时间】:2017-04-22 04:57:19
【问题描述】:
使用
"angular2-moment": "^1.3.3",
"moment": "^2.18.1",
当我在模板中使用amTimeAgo 管道时:
<div *ngFor="let issue of getIssues() | async ">
{{ issue.time | amTimeAgo }}
<!--{{ issue.time | amUtc }}-->
<!--{{ issue.time | amLocal }}-->
<p>{{ issue.time | amDateFormat:'dddd, MMMM Do YYYY, h:mm:ss a' }}</p>
{{ issue.time }}
{{issue | json }}
</div>
,具有可观察性:
getIssues(): Observable<any[]> {
return Observable.of([
{
name: "1",
time: new Date(),
},
{
name: "2",
time: new Date(),
},
{
name: "2",
time: new Date(),
}
]);
}
常量每秒刷新一次(每次都是 new new Date())。
如何预防?
为什么每次都获取新数据?
我不想每次都有新数据。另外,这将是一个 HTTP 调用,因此很昂贵。
我猜更改检测被触发太频繁了...
我检查的其他管道似乎不会导致问题(例如 amDateFormat)
【问题讨论】:
-
I guess change detection is triggered too often...这是正确的。有很多事情会触发变更检测周期。事实上,您可能会对它的触发频率感到非常惊讶。 -
我认为变更检测是由
amTimeAgo管道触发的。 -
很有可能。我希望你可以配置事件的间隔,因为频繁的事件可能会影响整个组件的性能。特别是,如果您的页面计算量很大...
标签: angular momentjs angular-moment angular-pipe