【问题标题】:AngularJS scope bug? Different results on chrome(43) and firefox(38.0.5)AngularJS范围错误? chrome(43) 和 firefox(38.0.5) 的不同结果
【发布时间】:2015-09-15 01:37:28
【问题描述】:

我最近在摆弄 angularjs,发现了一些奇怪的东西。

使用函数来确定存储在 ng-repeat 内部对象属性中的日期之间的日期差异,我在 chrome 上得到了不同的结果,在 firefox 上得到了不同的结果

PLUNKER
火狐的结果:http://scr.hu/28dp/17r4y(不正确)
来自 chrome 的结果:http://scr.hu/28dp/uik13(正确)

我用来计算时差的函数:

$scope.daysDiff = function (date) {
        var dateobj = new Date(date);
        var current = new Date("2015-06-28");
        var resultDays = Math.floor(Math.abs((current - dateobj) / (86400000))); //1k *60*60*24
        return  resultDays;
    };

我在 ng-repeat 中多次使用它来显示或隐藏元素,如下所示:

<tr ng-repeat="dat in data">
  ...
  <td><div class="inline" ng-show="daysDiff(dat.dateExpires) < 14">YES</div></td>
  ...
  <td><div class="inline" ng-show="daysDiff(dat.dateUpdate) < 14">YES</div></td>
  ...
</tr>

使用 ng-if 时的行为完全相同

//编辑

这个问题不是关于使用 angular 生成正确的表格,而是关于 chrome 和 firefox 之间的不一致。

问题仍然存在,由于某种原因,在 Firefox 中没有正确评估表达式。您可以在我上面附加的 plunker 示例中看到它。 尝试在 chrome 中打开它,然后在 firefox 中打开。

//编辑#2 为清楚起见添加了 td 标签

注意:同样的问题发生在 angular 1.3

【问题讨论】:

标签: javascript angularjs google-chrome firefox angularjs-ng-repeat


【解决方案1】:

问题出在 Date() 对象本身。chrome 能够解析日期 2015-07-7 没有任何问题,而 firefox 返回无效日期目的。 2015-07-07 工作正常。

工作示例 http://plnkr.co/edit/MnhLCohlWB3Nrc2QQ2Zi?p=preview

dateExpires: "2015-07-" + ((i<3) ? ("0"+(i + 7)) : (i + 7)), 

它只是在日期前添加了 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2014-05-10
    相关资源
    最近更新 更多