I read AngularJS documentation and also this question this vs $scope in AngularJS controllers.I get a part of my required answer here Difference between this and scope in the controller as well. I have a simple confusion regarding "this" and "$scope" in my code, I followed CodeSchool Tutorial suggested by Official AngularJS Page, and I wrote this code
Controller (Using 'this' as I practiced on Code School ):
var myApp = angular.module('myapp', []);
myApp.controller('TestController', function () {
this.name = 'Yawar Khan';
});
HTML:
<body ng-app="myapp">
<div ng-controller="TestController">
<h1> Hello {{name}} to AngularJS ! </h1>
</div>
</body>
And it is not evaluating the AgularJS expression and not outputting the value of name.
But if I use $Scope in my controller like
var myApp = angular.module('myapp', []);
myApp.controller('TestController', ['$scope', function ($scope) {
$scope.name = 'Yawar Khan';
}]);
Output is correct i.e. AgularJS expression {{name}} is evaluated as name value given in controller.
can anyone explain it to me that why everything worked perfectly on Code School tutorials with only 'this' ?