1

Found such an idea in article:

Notice how the value function takes the scope as parameter (without the $ in the name). Via this parameter the value function can access the $scope and its variables.

$scope.$watch( function( scope ) {
  return scope.val;
...

instead of what i used to:

$scope.$watch( function() {
  return $scope.val;
...

Is it really better? And what is the reasoning behind this way?

Super Babaca
  • 1,585
  • 2
  • 13
  • 15

1 Answers1

2

From AngularJs docs

function(scope): called with current scope as a parameter.

So it does not change the behavior of your code. However this version prevents a capture of the $scope variable inside the callback :

$scope.$watch(function(scope) {
    return scope.val;
}, function(value){ });
mathieu
  • 30,974
  • 4
  • 64
  • 90
  • can you explain please that "prevents a capture of the $scope variable inside the callback"? Or give an example! Thanks a lot! – Super Babaca Dec 03 '14 at 14:33