Reputation: 2812
I am a beginner in AngularJS.
I am working through some code of an expert. I would like to customize the directive and learn something.
The expert always insert:
this.scope = $scope;
in the first line of every controller.
What's the point of this statement, if you later always just use $scope
.
Upvotes: 2
Views: 77
Reputation: 20751
this
pointer was referring to $scope
instead of the controller.
courtesy of Mark Rajcok taken from How does 'this' and $scope work in AngularJS controllers
without this
app.controller('MyCtrl', function($scope){
$scope.doStuff = function(){
//Really long function body
};
});
with this
var MyCtrl = function($scope){
var _this = this;
$scope.doStuff = function(){
_this.doStuff();
};
};
MyCtrl.prototype.doStuff = function(){
//Really long function body
};
MyCtrl.$inject = ['$scope'];
app.controller('MyCtrl', MyCtrl);
Upvotes: 1