Use $scope or this in your controller?

51 views
Skip to first unread message

bargitta

unread,
Jun 22, 2015, 10:32:20 AM6/22/15
to ang...@googlegroups.com
Hi,

Do you use $scope in your controller to setup variables that will be used in the html template?
Or do you use this like this.user = {} in your UserController and ctrl as UserController, ctrl.user in the html template?
What is the recommended way? 

Pete Bacon Darwin

unread,
Jun 22, 2015, 10:36:39 AM6/22/15
to ang...@googlegroups.com
The Angular team are encouraging less use of the $scope object inside controllers and leaning towards the `controller as` style, where you use `this.user...`, since this makes you controllers even less reliant upon Angular implementation details (i.e. scope) and is also more aligned with how "component" directives are defined in Angualr 2.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Xi Chen

unread,
Jun 22, 2015, 10:42:15 AM6/22/15
to ang...@googlegroups.com
thanks Pete, but I remember that before I use "this", and then I was told to move to "$scope", since some say that $scope is the view model that combines your template and controller. Seems that suggestion is not a valid one.
But when I use "this.user={}", is the user stored in the controller or stored in the $scope internally?

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/JW5hgkCWRlQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
Message has been deleted

Sander Elias

unread,
Jun 22, 2015, 11:17:11 AM6/22/15
to ang...@googlegroups.com

Hi Bargitta,

Well, with time things change :)
The current controllerAs syntax exposes the controller to the local $scope. If you use ng-controller="myController as vm" behind the scenes something similar to this happens: $scope.vm = this;

So, to answer your question, your user is stored in the controller, and the controller is stored in the $scope. (it is all stored by reference, so there isn’t much of an memory issue with it!)

Regards
Sander

Luís Loureiro

unread,
Jun 22, 2015, 11:35:09 AM6/22/15
to ang...@googlegroups.com
Hi Bargitta.
It explains well how to use "controller As" syntax.

Regards,
Luís Loureiro

Xi Chen

unread,
Jun 22, 2015, 3:21:08 PM6/22/15
to ang...@googlegroups.com
thanks all for your comments. Do you know why angular tries to move "$scope" away?

--

Xi Chen

unread,
Jun 22, 2015, 3:24:12 PM6/22/15
to ang...@googlegroups.com
i also find that $scope will not be that easy to be removed. For example, if I want to use $scope. $watch.
Reply all
Reply to author
Forward
0 new messages