我有一个 Controller :
$scope.timeAgoCreation = function(order) {
return moment(order.createdAt).fromNow();
};
在 View 中:
{{timeAgoCreation(order)}}
它返回正确的值:9 分钟前。但是这个值不是实时更新的。我必须刷新页面。
是否可以让它实时更新?
最佳答案
只需将此功能添加到 Controller 中(不要忘记注入(inject) $timeout
服务):
function fireDigestEverySecond() {
$timeout(fireDigestEverySecond , 1000);
};
fireDigestEverySecond();
$timeout
在作为第一个参数传入的函数被调用后自动触发摘要循环,因此 View 中的值应该每秒更新一次。
你在那里工作 jsFiddle .
关于javascript - Angular : Update A Function In RealTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17641278/