S
S
srnsdlmtn2016-08-28 12:11:15
JavaScript
srnsdlmtn, 2016-08-28 12:11:15

How to pass multiple values ​​from $scope to view in angularjs?

There is a controller that, firstly, takes some data from json and saves it to a variable, and secondly, it counts the difference between two dates as a loop.

app.controller('myController', function($scope, loadData) {
  loadData.success(function (data) {
    $scope.Info = data;

    for(i=0; i<data.length; i++) {
      var dateFormat = 'DD/MM/YYYY HH:mm:ss';
      var end = moment(data[i].date);
      var now = moment();

      var difference = moment(endDate,dateFormat).diff(moment(currentDate,dateFormat));
      var duration = moment.duration(timeDIff);
      var formatDuration = timeDuration.format("d дня hh часов mm минут ss секунд");

      console.log(formatDuration);
      $scope.cd = formatDuration ;
    };
  });
});

Console.log displays all the values ​​in order, and when I save them to a variable and insert it into the view, only the last value is shown. I understand that they are overwritten one after the other, but I do not know how to make them go in order as the name value, which is taken from the json array.
<div>
  <div ng-repeat="item in Info">
    <h3>{{item.name}}</h3>
    <p>До окончания:<br />{{cd}}</p>
  </div>
</div>

PS: one more question, I will be grateful if I also get an answer to it, but rather a link that tells how a real-time counter is implemented in angular for about my case. That is, so that the time left until the end ticks in real time. I thought to use setInterval, but for some reason it seems that angular has its own things for this, which I can't find.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2016-08-28
@srnsdlmtn

$scope.calcTime = function(date){
      var dateFormat = 'DD/MM/YYYY HH:mm:ss';
      var end = moment(date);
      var now = moment();

      var difference = moment(endDate,dateFormat).diff(moment(currentDate,dateFormat));
      var duration = moment.duration(timeDIff);
      var formatDuration = timeDuration.format("d дня hh часов mm минут ss секунд");

      console.log(formatDuration);
      return formatDuration;
}

<div>
  <div ng-repeat="item in Info">
    <h3>{{item.name}}</h3>
    <p>До окончания:<br />{{calcTime(item.date)}}</p>
  </div>
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question