Basically, both the use of $scope
and this
in angle are intended to share information between view
and controller
.
The difference is that when using this
, you must use an alias for the controller in your view
.
For example:
<div ng-controller="MeuController as meu"></div>
The great advantage I see when using this
is that if you have multiple controllers within the other, and at some point need to name variables with the same name, this alias will act as a namespace
that will separate the values.
Note : When using this
without as
in the view (alias definition), you can access the information through the $ctrl
variable (at least this is how it works in component definition ).
In the case of $scope
, you can define your variables, being able to access them immediately, without needing a "prefix" as in the case above.
Note that in some cases, you will need $scope
, even using this
. For example, in case of setting a watcher
manually.
controller('MeuController', function ($scope) {
var vm = this;
vm.name = 'Wallace';
$scope.$watcher('vm.name', function () {
vm.first_name = name.split(' ')[0];
})
})
Not to mention that, among other information, $scope
contains special information such as $parent
, which references the parent controller of the current controller.