Injecting without constructor

733 views
Skip to first unread message

Paulo Coutinho

unread,
Jul 7, 2016, 3:55:47 PM7/7/16
to AngularJS
Hi,

When you create a service that have global properties, like:

import {Injectable} from "angular2/core";


@Injectable()
export class MyService {
   
private myValue;


    constructor
() {}


    setValue
(val) {
       
this.myValue = val;
   
}


    getValue
(val) {
       
return this.val;
   
}
}

And you need access it globally from your app initialising:

import {MyService} from './services/my.service';
bootstrap
(App, [MyService, COMMON_DIRECTIVES, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, HTTP_PROVIDERS]); // directives added here are available to all children

And you need access it from your component:

import {MyService} from '../services/my.service';


@Component({
    selector
: 'some-component',
   
template: `
<div>MyValue: {{val}}</div>
`

})
export class SomeComponent {
    constructor
(private myService:MyService) {
   
}


   
get val() {
       
return this.myService.getValue();
   
}
}

You always create a constructor with private variable creation as the custom service. No problem here.

My question is about the possibility of create the component, with the services injected, but without a constructor and private param.

Thanks.










Sander Elias

unread,
Jul 8, 2016, 3:24:03 AM7/8/16
to AngularJS
Hi Paulo.

May I ask for your use-case? do you wan't/need some kind of global?
I'm not aware of a way to do it through the injector. However, it is possible to create a real singleton service(that you can use in and outside angular), that you put into the global space. 

regards
Sander

Paulo Coutinho

unread,
Jul 8, 2016, 12:58:52 PM7/8/16
to ang...@googlegroups.com

Hi,

I need a global service with variables that dont change across the application.

You can see my project as an example:

https://github.com/prsolucoes/gohc


I have on header the count/list of running jobs. I need that every second this new informations change on this global service and this information will be used to show the list of running jobs too.


Thanks.




--
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/shzB6YcCHZ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Lucas Lacroix

unread,
Jul 8, 2016, 1:01:24 PM7/8/16
to ang...@googlegroups.com
My understanding is that all injected dependencies are singletons.
So, if you inject the same type of thing in multiple places, you should receive the single instance of that thing.

--
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 https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Lucas Lacroix
Computer Scientist
System Technology Division, MEDITECH

Paulo Coutinho

unread,
Jul 8, 2016, 1:03:51 PM7/8/16
to ang...@googlegroups.com
Ok,

But the question is about receiving this "injection". 

The only way to inject my global service is from the constructor? Or i can create a component with any constructor and on "OnInit" i set "this.myService = ???".

Thanks.

Atenciosamente, 
Paulo Coutinho.
PRS - Soluções Inteligentes
Blog: www.pcoutinho.com 
Skype: paulo.prsolucoes

Lucas Lacroix

unread,
Jul 8, 2016, 1:09:56 PM7/8/16
to ang...@googlegroups.com
It sounds like you want to inject the service into all components, which isn't possible to do automatically and, I would argue, would be very bad.

You have to reference the injectable in your component's constructor.

The other solution, as Sander said, is to create your service in the global namespace (ex. "window.myGlobalService = ...") and you can then reference it everywhere.

Reply all
Reply to author
Forward
0 new messages