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.

1
  • Seems like if a question is marked as a dupe, a link to the original should be provided. Commented Jan 8, 2018 at 21:21

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
well, technically you are watching salesprice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.