1

I have a small AngularJS app which searches and retrieves a listing of users and their next scheduled meeting (assuming one is scheduled in the next 8 hours) brought back from the server using JSON with the time format in UTC for ease of calculation to local times. I have the javascript written to be able to calculate the time remaining, what I do not have written or figured out is how to do this within Angular.

What I would like to accomplish is to be able to update the DOM with time remaining until the meeting scheduled has completed or time left until the meeting starts. It seems straight forward if updating a single element using the $timeout service, however I am not clear on how to accomplish this task within an ng-repeat for several items at a time.

Any ideas would be appreciated! Thanks!

2 Answers 2

2

in the ng-repeat, you'll have a list of items.

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

Try updating just the $scope.currentTime and letting each item have a timeUntil method. You'll just update the $scope.currentTime from your timeout. Angular will handle the rest.

The key thing here is knowing that you are just putting in an "expression" and in angular, an expression can be complex,even involving a function call.

Sign up to request clarification or add additional context in comments.

1 Comment

This will work perfectly, just need to set a $timeout on the update and it should work as I want it to. Didn't realize you can add functions from the controller within the ng-repeat directive. Thanks!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.