1

我有一个小型 AngularJS 应用程序,它使用 JSON 和 UTC 时间格式从服务器带回用户列表和他们的下一次预定会议(假设在接下来的 8 小时内安排),以便于计算到本地时间. 我编写了能够计算剩余时间的 javascript,但我没有编写或想出的是如何在 Angular 中执行此操作。

我想要完成的是能够在预定会议完成之前的剩余时间或会议开始之前的剩余时间更新 DOM。如果使用 $timeout 服务更新单个元素似乎很简单,但是我不清楚如何在一次 ng-repeat 中为多个项目完成此任务。

任何想法,将不胜感激!谢谢!

4

2 回答 2

2

duplicate question, answered here: https://stackoverflow.com/a/16204830/2275421

http://plnkr.co/edit/0JqK96irV4ETdWZYxO3P?p=preview

changed the html to refer to a separate array that doesn't affect ng-repeat:

<td>in {{_timeLeft[$index]}}</td>

which is updated as follows:

$scope._timeLeft = [];

var intervalID = window.setInterval(function() {
    for (var i=0; i<$scope.items.length; i++) {
      $scope._timeLeft[i] = $scope.timeUntil($scope.items[i]._freeBusyTime);
    }
    $scope.$apply();
}, 1000);

Note that $scope.$apply() is required to let Angular know that '_timeLeft' has been modified, which updates all references to it.

于 2013-04-25T05:26:34.520 回答
2

在 ng-repeat 中,您将拥有一个项目列表。

<li ng-repeat="items" >{{ timeUntil(currentTime) }}</li>

尝试只更新 $scope.currentTime 并让每个项目都有一个 timeUntil 方法。您只需从超时中更新 $scope.currentTime。Angular 将处理剩下的事情。

这里的关键是知道你只是在输入一个“表达式”,并且在角度上,一个表达式可能很复杂,甚至涉及一个函数调用。

于 2013-04-24T19:20:33.790 回答