Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Yo everyone.

I've got a .json file structured like that :

var var1 =
[ { "a":"soluce1",
    "b": 
           [ { "user1" : "username1",
               "ba" : "1" },
             { "user2" : "username2",
               "ba" : "2" } ],
    "c":"useless"
} ];

, my .html file :

<tr ng-repeat="varhtml in var1">
    <td>{{varhtml.b[0].ba}}</td>
</tr>

and it appears :

1


I know that all is good at first. For the next step, I would display

3

, result of (1+2 : (ba+ba and more arrays if needed...)). At first glance I thought that was not really difficult but I don't know what I need to do with <td>{{varhtml.b[0].ba}}</td> or others files to have this wish.

Thank you in advance for the helps.

WORKING RESULT :

$scope.additionNumber = function (varhtml) {    
var total = 0;  
  angular.forEach (varhtml.b, function (ligne) {        
     total += ligne.ba;     
  });   
return total; 
}


<tr ng-repeat="varhtml in var1">
    <td><span ng-bind="additionNumber(varhtml)"></span></td>
</tr>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Why not precalculate the value and set that on the scope?

Don't entirely understand your question but if you want a sum of all the values then you could precalculate the value, or if it needs to be dynamic, calculate it with a method?

$scope.calculateValue = function() {
    // loop through array and add up the values
    // then return the value
    return value;
};

<span ng-bind="calculateValue()"></span>

If you just need to xn + xn+1 then you could use a for loop and loop through b and do some logic?

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.