Reputation: 1644
I am new to Angular JS and have been taught it the 'this' way:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
var app = angular.module('Comment',[]);
app.controller('CommentCtrl',function(){
this.welcome = 'Hello!';
});
</script>
<p ng-app='Comment' ng-controller='CommentCtrl as ctrl'>
Angular says: {{ ctrl.welcome }}
</p>
Which shows 'Angular says: Hello!' inside the paragraph.
However, every angular application I have ever seen has used '$scope' instead of 'this', like I was taught.
Could someone please explain a few of the pros and cons of each, and what exactly $scope is in terms that I can understand?
Thanks.
Upvotes: 0
Views: 141
Reputation: 8325
Some Good Points:
this
this
is the controller.this
is the "scope in effect when the function was called". This may (or may not!) be the $scope that the function is defined on. So, inside the function, this
and $scope may not be the same.Here is cool article by Todd Motto
Happy Helping!
Upvotes: 3