1

I am new to AngularJS and want to find an efficient solution for my problem.

I have an array of objects as

var list = [
{listprice: 100, salesprice:100, discount:0},
{listprice: 200, salesprice:200, discount:0},
{listprice: 300, salesprice:300, discount:0},
];

Salesprice can be changed by the user or updated by backend code. My requirement is to watch each of the salesprice for changes(whether UI or backend) and update the corresponding discount for that object only.

I was considering using $watch, but can not understand how to use it to bind to each salesprice in the array and the corresponding function that changes the discount.

Drake
  • 11
  • 2
  • 5

1 Answers1

2

granted list is in your $scope you would simply watch via:

$scope.$watch('list', function(newVal, oldVal){
    console.log('changed');
},true);

Note the last parameters of true for deep watching is required. The third is optional and is referring to objectEquality https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch

Ronnie
  • 11,138
  • 21
  • 78
  • 140
  • Thanks for responding, but this does not answer my question. I need to $watch only the salesprice not the entire list and need to update the discount for the salesprice that has changed. – Drake Oct 24 '15 at 01:07
  • well, technically you are watching salesprice – Ronnie Oct 25 '15 at 23:47