Search code examples
angularjsdata-bindingcomputed-values

angular nested data object computed value binding


I have a table of data, a list of staff and their salaries for each month of the year where there is a total for each month across all staff which is calculated dynamically (sum of salaries that fall in that month).

I am trying to get it so that when I change one of the staff salaries for a particular month the calculated total for that month re-evaluates and therefore angular will update the ui but as this total property is computed I can't see a way to 'mark it as dirty' so angular knows to re-evaluate?


Solution

  • Your scenario sounds like it could use a custom filter.

    Something close to:

    custom filter:

    angular.module('myApp', []).filter('mySumFilter', function() {
      return function(items, param1, param2) {
        return //perform sum items object in case of necessary use params to narrow your operation
      };
    });
    

    and in your html

     <div> {{ salaryArray |mySumFilter:'salary': 2"}} </div>
    

    Of course you could extend it and perform the actions that are more suited to your app needs.