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.