Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I can't understand why the next code is not working.

<div ng-repeat="r in reservations" ng-init="new = r.some_code">

<div ng-if="old == new">
  <p ng-init="sum = sum + r.cost">{{r.name}} - {{r.cost}}</p>
</div>

  <div ng-if="old != new">
      <p ng-init="old = new;">Total: {{sum}}</p>
  </div>

</div>

Before that code I have an array reservations like the following:

reservations = [{name: client 1, some_code = 15, cost: 1000},
{name: client 1, some_code = 15, cost: 1000},
{name: client 2, some_code = 15, cost: 1000},
{name: client 3, some_code = 16, cost: 2000},
{name: client 4, some_code = 16, cost: 3000},
{name: client 5, some_code = 17, cost: 3000}]

$scope.old = reservations[0].some_code;
$scope.sum = 0;

As a result I get something like:

15 - client 1 - 1000 15 - client 2 - 1000 15 - client 3 - 1000 Total: 0 Total: 0

I tried a bunch of times but with different wrong results. What I want is to display each reservation and if "some_code" changes, show the total.

How Can I do this?

NOTE: the array is sort by some_code.

share
    
is reservations array in the scope ? $scope.reservations = [..] – Gonzalo.- 7 secs ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.