0

I have an angularjs controller. I added a function to it's scope, since this function is called from the html.

$scope.search = function() {
    //do something
};

But I need to call this function from inside my controller too. I can do this by either calling

$scope.search();

or

this.search();

Both work. It's clear form me that it works, calling on the $scope, but why does it work calling on this? As far as I understand, the $scope is a dedicated object. Also, what's the preferred way of calling the function from within the controller?

danielgehr
  • 127
  • 1
  • 6
  • 1
    Explained here: http://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers – salix Dec 16 '16 at 08:49
  • 1
    these **are not equivalent**, unless there is some other code which you are not showing that makes them equivalent. – Claies Dec 16 '16 at 08:49
  • The link posted by salix answers my question. I called `this.search()` from within my scope function. Therefore `this` referred to `$scope` and not to my controller. – danielgehr Dec 16 '16 at 09:13

0 Answers0