Angular.js - 使用间隔应用过滤器 [英] Angular.js - Apply filter using an interval

查看:224
本文介绍了Angular.js - 使用间隔应用过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义的angular.js过滤器datetime对象:

I am using a custom angular.js filter for datetime objects:

function relativeTimeFilter()
{
    return function (dateObj) {
        return getRelativeDateTimeString(dateObj);
    };
}

function getRelativeDateTimeString(dt)
{
    if(!dt) return "undefined ago";

    var delta = dt.getSeconds();
    if (delta < 0) return "not yet";
    if (delta < 1 * 60) return delta == 1 ? "one second ago" : delta + " seconds ago";
    if (delta < 2 * 60) return "a minute ago";
    if (delta < 45 * 60) return Math.floor(delta/60) + " minutes ago";
    if (delta < 90 * 60) return "an hour ago";
    if (delta < 24 * (60*60)) return Math.floor(delta/60/60) + " hours ago";
    if (delta < 48 * (60*60)) return "yesterday";
    if (delta < 30 * (24 * (60*60))) return Math.floor(delta/60/60/24) + " days ago";
    if (delta < 12 * (30 * (24 * (60*60))))
    {
        var months = Math.floor(delta/60/60/24/30);
        return (months <= 1) ? "one month ago" : (months + " months ago");
    }
    else
    {
        var years = Math.floor(delta/60/60/24/365);
        return (years <= 1) ? "one year ago" : (years + " years ago");
    }
}

module.filter("relativetime", relativeTimeFilter);

在这一点上,我使用的过滤器(我认为)不是那么重要。该过滤器接收DateTime对象。相对时间声明仅适用一秒。含义一秒前必须经过第二次更新,以 2的秒前等。

At this point, it is not so important which filter I use (I think). The filter receives a Datetime object. The relative time declaration is only valid one second. Meaning one second ago must be updated after a second to 2 seconds ago and so on.

在我申请的过滤器,这只会发生一次。那么,如何触发隔一段时间就进行一次滤波装置?

When I apply the filter, this only happens once. So how do I trigger the filter appliance in a regular interval?

我试过如下:

setInterval(function() {$scope.$apply()}, 1000) // placed in controller function

...,但没有成功。

...with no success.

任何想法?

推荐答案

我不认为你将能够使用过滤器来实现这一目标。究其原因 $范围。$适用()行不通,是因为它是看的上的数据变化。由于数据实际上并没有改变,过滤器永远不会被再次调用。

I don't think you are going to be able to achieve this with a filter. The reason $scope.$apply() doesn't work is because it is watching for changes on the data. Since the data didn't actually change, the filter never gets called again.

相反,你需要调整在控制器正在查看的数据。使用 $超时而不是的setInterval ,因为它是建立在消化周期。

Instead, you need to adjust the data that is being viewed in your controller. Use $timeout instead of setInterval because it is built into the digestion lifecycle.

我会考虑使用一个指令,要做到这一点。

I'd consider using a directive to do this.

app.directive('relativeTime', function($timeout) {

  function update(scope, element) {
    element.text(getRelativeDateTimeString(scope.actualTime));
    $timeout(function() { update(scope, element); }, 1000);
  }

  return {
    scope: {
      actualTime: '=relativeTime'
    },
    link: function(scope, element) {
      update(scope, element);
    }
  };
});

所以,你可以使用该指令是这样的:

So you can use the directive like this:

<div relative-time="theDate"></div>

另外,我发现在你的 getRelativeDateTimeString 功能的缺陷。你需要你的基地关闭三角洲的当前时间。 getSeconds 只是给你的给定时间的秒数:

Also, I found a flaw in your getRelativeDateTimeString function. You need to base your delta off of the current time. getSeconds just gives you the seconds of the given time:

var delta = parseInt(((new Date().getTime()) - dt.getTime()) / 1000);

下面是一个工作 codePEN

这篇关于Angular.js - 使用间隔应用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆