JavaScript angular: $watch set in a loop angular watch loop,angularjs watch loop

I do the following

 $scope.product.orders.forEach(function (order) {
    $scope.$watch('order.details.items.length', function (n, o) {
       if (n !== o) {
          //do something
        }
     });
  }, true);

where details is an object and items is an array.

The $watch is triggered once for every orders in the collection, with n and o being undefined.

Then whenever I add an item to the items array, the $watch is not triggered. Why is that ? There is no errors in the log.

note: I know that creating a $watch in a loop is clearly not a good thing performance-wise, it's a workaround for now.

Answer:1

If the first parameter of $scope.$watch is a string, it defines an expression that is evaluated on your scope. Usually this is a scope variable. In your case order is not a scope variable but a parameter of the forEach's handler function. You have to use the function way in this case:

 $scope.product.orders.forEach(function (order) {
    $scope.$watch(function () {
       return order.details.items.length;
    }, function (n, o) {
       if (n !== o) {
          //do something
        }
     });
  }, true);
Answer:2

I am trying to get the image zooming effect using JQuery. I have only one image that is of higher resolution, When i click any part of that image, i want to get the zoomed view of that part. For ...

I am trying to get the image zooming effect using JQuery. I have only one image that is of higher resolution, When i click any part of that image, i want to get the zoomed view of that part. For ...

With the way I have my html and javascript set up right now, I have it so when the page loads up and shows up all the text under each button. You can click the button to hide and show the text right ...

With the way I have my html and javascript set up right now, I have it so when the page loads up and shows up all the text under each button. You can click the button to hide and show the text right ...

I have a piece of code, which communicate with iOS native layer by dispatching request with callback ID, and storing Deferred in array. Then when the callback comes, we can resolve or reject this ...

I have a piece of code, which communicate with iOS native layer by dispatching request with callback ID, and storing Deferred in array. Then when the callback comes, we can resolve or reject this ...

I'm currently using enyo and I have a function on a component that takes a callback function and makes an ajax call, then calls the callback on success. I can't seem to figure out how to spy on the ...

I'm currently using enyo and I have a function on a component that takes a callback function and makes an ajax call, then calls the callback on success. I can't seem to figure out how to spy on the ...

  1. ajax callback
  2. ajax callback function
  3. ajax datatable callback
  4. call ajax callback
  5. ajax callback error
  6. callback after ajax
  7. callback function ajax not working
  8. jquery ajax callback
  9. html callback ajax
  10. get callback ajax jquery
  11. using callback ajax
  12. call callback function ajax
  13. call back method in ajax
  14. ajax callback in oracle apex
  15. ajax call back button
  16. ajax callback example
  17. callback function in jquery ajax
  18. ajax callback drupal 8
  19. ajax callback drupal 7
  20. ajax callback data