0

I have a output records,

  "Records": [{"successcount":3,"tap":"DF"},{"successcount":6,"tap":"MF"}] in this format.

In my html page, I need to add those successcount & display.

My HTML:

   <ng-container *ngFor="let item of element.Records; let i=index">
      {{ item?.successcount }}
      {{ i }}
   </ng-container>

Here Im able to display successcounts separately. But I need to add both of them in html itself and display. Can anybody tell me how to do this?

2
  • 1
    why you dont want to do the calculation on .TS file? Commented Oct 16, 2019 at 8:43
  • Also as a suggestion, I would try to avoid using functions in interpolation(html) as they would get evaluated at every change detection. So you could try to that calculation in .ts and display the result here.
    – KiraAG
    Commented Oct 16, 2019 at 9:17

1 Answer 1

0

No need to use *ngFor over the array as you don't need to display anything for each item. You can call a function from HTML and display count like this -

<div>{{totalSuccessCount()}}</div>

totalSuccessCount() {
   element.Records.reduce((a, b) => a.successcount + b.successcount);
}

Best approach would be in this case you calculate count in controller and store the value in some variable and print it.

PS: But calling a function in template is not recommended in angular, as it will be calling on every change detection which may lead to slows down the performance of your application.

1
  • thanks for the reply. I have so many Records like this. Thats why I have I used for loop.
    – viki
    Commented Oct 16, 2019 at 8:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.