Basically, both the use of $scope
like the this
is intended to share information between view
and controller
.
The difference is, when you use this
, you need to use an alias for the controller in your view
.
For example:
<div ng-controller="MeuController as meu"></div>
The great advantage I see in using the this
, is that if you have multiple controllers inside each other, and at some point need to name variables with the same name, this alias will act as one namespace
that will separate the values.
Note: When using this
without the as
in the view (alias setting), you can access the information via the variable $ctrl
(at least that’s how it works in definition of components).
In the case of $scope
you will be able to define your variables, being possible to access them immediately, without need of a "prefix" as in the case above.
Note that in some cases, you will need the $scope
, even using the this
. For example, in the case of a definition of 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, the $scope
contains special information such as $parent
, referencing the parent controller of the current controller.
There is already an equal topic only in English.... Is the reason to create this just to increase reputation? https://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers
– djva
@djva the reason to create is to have content in Portuguese, after all this is the Stackoverflow in Portuguese. Otherwise there would be the need for the existence of this site, since most of the questions have already been answered there. Follows here an answer that clarifies a little more your doubt.
– Sorack
So if the meaning is to translate, we plagiarize content and translate it?
– djva
@Djava Follows the definition of plagiarism. I don’t see how a question can be an intellectual property, especially considering different audiences. Nor can I understand why someone like you, who has the necessary knowledge to conduct a survey in English, would participate in a forum in Portuguese, since practically all the answers are already available in another language. And as for your comment, feel free to open a question on meta which is aimed at doubts of this kind.
– Sorack
Translation is cool with moderation @djva, the manager of our community clarified that once already..
– BrTkCa
Whenever you have any questions about any conduct, it’s good to see the goal, to see what the community thinks.
– Wallace Maxters