角过滤的结果在UI中没有更新 [英] Angular filtered result not updating in the UI

查看:157
本文介绍了角过滤的结果在UI中没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的过滤器时刻:

I have this filter for moment:

function momentFilter() {
  return function(input, format) {
    if (!input) return null;

    if (format == 'fromNow') {
        var fn =  moment(input).fromNow();
        console.log('fromNow called with: ' + input + ' giving result: ' + fn);
        return fn;
    } 
  };
}

和我有这个网站:

<div>{{timestamp | moment: 'fromNow'}}</div>

我看到越来越过滤叫上消化周期,并得到在控制台登录的相对时间看起来不错。 I.E.从1分钟前的推移,2分钟前,等等......然而UI永远不会更新到什么过滤器返回。

I see the filter getting called on the digest cycle, and the relative time that gets logged in the console looks great. I.E. it goes from 1 minute ago, 2 minutes ago, etc... However the UI never updates to what the filter returns.

第一个电话被反映在用户界面,但在此之后,UI从来没有更新。

The first call is reflected on the UI, but after that the UI never updates.

我在做什么错了?

编辑:

这是HTML是NG重复,通过ID追踪中。我假设,因为没有在对象实际上改变(当前时间刚刚搬进),即角从未检测到该对象的任何事情。

That html is inside an ng-repeat that tracks by id. I am assuming that because nothing in the object actually changed (current time just moved), that angular never detects anything for that object.

不过不知道如何解决这个问题。

Still not sure how to work around this.

推荐答案

假设过滤器为幂等,即同一输入总是会导致相同的输出。由于时间戳并没有改变,没有理由对角做任何事情。

Filters are assumed to be idempotent, that is the same input always leads to the same output. Since timestamp doesn't change there's no reason for Angular to do anything.

如果你的过滤器是有状态和输出可以与每一个电话改变,那么你必须相应地对它进行标记。

If your filter is stateful and the output can change with every call then you have to mark it accordingly.

function momentFilter() {
  function filter(input, format) {
    if (!input) return null;

    if (format == 'fromNow') {
      var fn =  moment(input).fromNow();
      console.log('fromNow called with: ' + input + ' giving result: ' + fn);
      return fn;
    } 
  };

  filter.$stateful = true;
  return filter;
}

这篇关于角过滤的结果在UI中没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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