简体   繁体   中英

How to calculate sum on a row in AngularJS without triggering digest cycle?

I have an array of objects which are each represented by a row in a table. One of the object values is "amount". So the array looks something like this:

var array = [{name: 'bob', amount: 10}, {name: 'james', amount: 12});

At the top of the table, I have a function calculate the sum of all the amounts and print it there. Unfortunately, it looks like this function is running every time some value is changing on the page even if it has nothing to do with that array. Is there any way to make the function called only when the array is updated?

You could watch the values of interest in your controller and only then calculate the sum and update it as a separate value on your scope.

Alternatively, as a perhaps easier optimisation, you can consider debouncing the calculation. This would greatly reduce how often it is being re-calculated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM