Regarding, the link / article at the start of this question: http://www.technofattie.com/2014/03/21/five-guidelines-for-avoiding-scope-soup-in-angular.html
It would definitely be interesting to get consensus on anyone with practical experience on whether using $scope the pre Angular 1.2 way should be unofficially deprecated in favour of the "this." approach, and using "Ctrl as .." declaration syntax inside template html. Am thinking in particular regarding maintainability and ease of testing benefits etc. Basically, is there consensus on a universally accepted best practice here? Almost every resource I have learnt from has used anonymous functions for Controllers, and injected a $scope reference. And so far, haven't noticed any $scope soup as I am splitting a large scale app up into discrete and focused modules. Insights would be welcome. What I'd like to know if it is just a question of personal preference, or whether the benefits are very real. Thanks in advance for any feedback. On 29 Apr 2014, at 15:11, Tony pee <[email protected]> wrote: > I dont think it really matters that they are revealed, but if you prefer to > use the controller like a factory: > > mod.controller('myCtrl', function($scope, $service1, $hat, $cat) { > function MyCtrl() { > //init > }; > MyCtrl.prototype.onSomething = function() { > $hat.color = 'red'; > }; > return new MyCtrl(); > }); > > Or you could import them to a variable like i discussed here: > > https://groups.google.com/forum/#!topic/angular/orpAMEbFq_w > > > > > On 28 April 2014 22:54, divisivecottonwood <[email protected]> wrote: > It does make perfect sense. I was just wondering if there was a way of using > services without assigning it as a property to the controller, like below but > cleaner > > Also, why aren't angularjs services like $log exposed to the template? > > var app = angular.module('myApp'); > > var _Ctrl; > > var Ctrl = function($rootScope, $log, $timeout, aService, CONFIG) { > this.$rootScope = $rootScope; > this.$log = $log; > this.$timeout = $timeout; > _Ctrl.aService = aService; > _Ctrl.CONFIG = CONFIG; > }; > > Ctrl.$inject = ['$rootScope', '$log', '$timeout', 'aService', 'CONFIG']; > > On Tuesday, 29 April 2014 05:49:34 UTC+1, tonypee wrote: > the 'controller as' feature simply adds your controller 'as' a variable on > the $scope, so that you can reference it. This means that you can set values > to the controller via 'this.myVar = xxx' and then reference them as > myCtrl.myVar in the template. (where you have set > ng-controller="SomethingCtrl as myCtrl") > > So, anything that you assign to the controller is exposed to the template. > You are assigning the services as properties of the controller.. so this > makes perfect sense - no? > > > > > On 28 April 2014 12:13, divisivecottonwood <[email protected]> wrote: > I've been experimenting with different code design patterns in my controllers > and services > > My starting point was Josh Carroll's 5 Guidelines For Avoiding Scope Soup in > Angular > [http://www.technofattie.com/2014/03/21/five-guidelines-for-avoiding-scope-soup-in-angular.html] > > As an example of a controller > > var app = angular.module('myApp'); > > var Ctrl = function($rootScope, $log, $timeout) { > this.$rootScope = $rootScope; > this.$log = $log; > this.$timeout = $timeout; > }; > > Ctrl.$inject = ['$rootScope', '$log', '$timeout'']; > > Now, if I was to use a code like this in the controller and inject a config > object of constants or my own service, like this: > > var app = angular.module('myApp'); > > var Ctrl = function($rootScope, $log, $timeout, aService, CONFIG) { > this.$rootScope = $rootScope; > this.$log = $log; > this.$timeout = $timeout; > this.aService = aService; > this.CONFIG = CONFIG; > }; > > Ctrl.$inject = ['$rootScope', '$log', '$timeout', 'aService', 'CONFIG']; > > When I use AngularJS Batarang to examine the models I see that aService or > CONFIG is being added to the scope model. > > It's like, hold on, I want to use the values or methods of the constants or > service, not add them to the model as well. > > I'm clearly getting confused here. Can somebody provide me with some guidance > about how to use a class-based approach without creating unnecessary models > in the process > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > > Visit this group at http://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. > > > > -- > Tony Polinelli > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. > > > > -- > Tony Polinelli > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
