Using Services in non-Angular ClassesAngularJS : How to watch service variables?Angular HTML bindingAngular 2 RC5: How to inject a service in another service dynamically? (plugin architecture)Import node js module in angular 2 applicationHow to properly import/export classes across angular modules?Angular HTTP request error: “post valid request”Dependency injection in angular hybrid app (AngularJS & Angular 5)$Injector Error on Angular Upgrade from 1.6.6 to 6ng: ERROR: Can't resolve all parameters for component when trying to pass context into ComponentPortalCore module component and Shared module implementation in angular
When did G-d say to Yaakov that he will have 12 sons?
What magic extends life or grants immortality?
Are there account age or level requirements for obtaining special research?
Why do all fields in a QFT transform like *irreducible* representations of some group?
In an emergency, how do I find and share my position?
Do AT motherboards (286, 386, 486) really need -5V (besides redirecting it to ISA connectors)?
In the MCU, why does Mjölnir retain its enchantments after Ragnarok?
Why can't an Airbus A330 dump fuel in an emergency?
How do I request a longer than normal leave of absence period for my wedding?
Potential new partner angry about first collaboration - how to answer email to close up this encounter in a graceful manner
Mixing basis sets
1980's sci fi book series, subspace is a sponge, young man in emergency suit can propel himself by sucking in the sponge stuff
Why were movies shot on film shot at 24 frames per second?
On the feasibility of space battleships
How much code would a codegolf golf if a codegolf could golf code?
Why we don't have vaccination against all diseases which are caused by microbes?
What does どうかと思う mean?
The remote server returned an error: (401) Unauthorized: While uploading file in document library using App Based Token Authentication
Justifying the use of directed energy weapons
how to solve two inequalities who have simultaneous answers
What brought these couples together?
Lying in Wellness program
Why do options retain significant extrinsic value after market closing on day of expiration?
Can anyone recognise the location and uniforms in these pictures
Using Services in non-Angular Classes
AngularJS : How to watch service variables?Angular HTML bindingAngular 2 RC5: How to inject a service in another service dynamically? (plugin architecture)Import node js module in angular 2 applicationHow to properly import/export classes across angular modules?Angular HTTP request error: “post valid request”Dependency injection in angular hybrid app (AngularJS & Angular 5)$Injector Error on Angular Upgrade from 1.6.6 to 6ng: ERROR: Can't resolve all parameters for component when trying to pass context into ComponentPortalCore module component and Shared module implementation in angular
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am using Angular v7.3.5 and I want to use a Service in a non-Angular class, something like this:
foo.model.ts
import Foo2Service from './foo2.service';
// Definition for FooClass (a model)
export class FooClass
constructor(args: any)
// Do something with args
func1()
// -----> Use Foo2Service here <-------
foo2.service.ts
export class Foo2Service
// Service code
constructor(private bar: BarService)
init()
// Code that returns something
app.component.ts
import FooClass from './foo.model.ts';
export class AppComponent
constructor()
const foo = new FooClass('bar');
console.log(foo.func1());
Is it possible to do so? If yes, what is the best way to do it?
NOTE: I tried to use the Injector class provided by Angular but it didn't work for me. So please help.
add a comment |
I am using Angular v7.3.5 and I want to use a Service in a non-Angular class, something like this:
foo.model.ts
import Foo2Service from './foo2.service';
// Definition for FooClass (a model)
export class FooClass
constructor(args: any)
// Do something with args
func1()
// -----> Use Foo2Service here <-------
foo2.service.ts
export class Foo2Service
// Service code
constructor(private bar: BarService)
init()
// Code that returns something
app.component.ts
import FooClass from './foo.model.ts';
export class AppComponent
constructor()
const foo = new FooClass('bar');
console.log(foo.func1());
Is it possible to do so? If yes, what is the best way to do it?
NOTE: I tried to use the Injector class provided by Angular but it didn't work for me. So please help.
Where is thisbarinstance coming from? (I am referring to codenew FooClass(bar);)
– Igor
Mar 27 at 20:21
@Igor Oh, sorry! It was intended to be astringor anobject. Fixed.
– Harshul Vijay
Mar 28 at 15:28
add a comment |
I am using Angular v7.3.5 and I want to use a Service in a non-Angular class, something like this:
foo.model.ts
import Foo2Service from './foo2.service';
// Definition for FooClass (a model)
export class FooClass
constructor(args: any)
// Do something with args
func1()
// -----> Use Foo2Service here <-------
foo2.service.ts
export class Foo2Service
// Service code
constructor(private bar: BarService)
init()
// Code that returns something
app.component.ts
import FooClass from './foo.model.ts';
export class AppComponent
constructor()
const foo = new FooClass('bar');
console.log(foo.func1());
Is it possible to do so? If yes, what is the best way to do it?
NOTE: I tried to use the Injector class provided by Angular but it didn't work for me. So please help.
I am using Angular v7.3.5 and I want to use a Service in a non-Angular class, something like this:
foo.model.ts
import Foo2Service from './foo2.service';
// Definition for FooClass (a model)
export class FooClass
constructor(args: any)
// Do something with args
func1()
// -----> Use Foo2Service here <-------
foo2.service.ts
export class Foo2Service
// Service code
constructor(private bar: BarService)
init()
// Code that returns something
app.component.ts
import FooClass from './foo.model.ts';
export class AppComponent
constructor()
const foo = new FooClass('bar');
console.log(foo.func1());
Is it possible to do so? If yes, what is the best way to do it?
NOTE: I tried to use the Injector class provided by Angular but it didn't work for me. So please help.
edited Mar 28 at 15:28
Harshul Vijay
asked Mar 27 at 16:40
Harshul VijayHarshul Vijay
3292 silver badges14 bronze badges
3292 silver badges14 bronze badges
Where is thisbarinstance coming from? (I am referring to codenew FooClass(bar);)
– Igor
Mar 27 at 20:21
@Igor Oh, sorry! It was intended to be astringor anobject. Fixed.
– Harshul Vijay
Mar 28 at 15:28
add a comment |
Where is thisbarinstance coming from? (I am referring to codenew FooClass(bar);)
– Igor
Mar 27 at 20:21
@Igor Oh, sorry! It was intended to be astringor anobject. Fixed.
– Harshul Vijay
Mar 28 at 15:28
Where is this
bar instance coming from? (I am referring to code new FooClass(bar);)– Igor
Mar 27 at 20:21
Where is this
bar instance coming from? (I am referring to code new FooClass(bar);)– Igor
Mar 27 at 20:21
@Igor Oh, sorry! It was intended to be a
string or an object. Fixed.– Harshul Vijay
Mar 28 at 15:28
@Igor Oh, sorry! It was intended to be a
string or an object. Fixed.– Harshul Vijay
Mar 28 at 15:28
add a comment |
1 Answer
1
active
oldest
votes
Using Injector should work:
Create the injector:
const injector = Injector.create(
providers: [
provide: Foo2Service, deps:[] ,
]
);
For the sake of the test, let's return the string test from your init function in service:
init()
return 'test';
for testing, in your class you would call init using the injector:
func1()
let myService = injector.get(Foo2Service);
return myService.init();
and finally the component calling func1:
ngOnInit()
const foo = new FooClass();
console.log(foo.func1()) // prints 'test'
DEMO
OK, now I know what I was doing wrong... Actually I had already did this, but not in 'Foo2Service'... Thanks, by the way...
– Harshul Vijay
Mar 28 at 15:27
You are very welcome, glad I could help! :)
– AJT_82
Mar 28 at 18:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382379%2fusing-services-in-non-angular-classes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using Injector should work:
Create the injector:
const injector = Injector.create(
providers: [
provide: Foo2Service, deps:[] ,
]
);
For the sake of the test, let's return the string test from your init function in service:
init()
return 'test';
for testing, in your class you would call init using the injector:
func1()
let myService = injector.get(Foo2Service);
return myService.init();
and finally the component calling func1:
ngOnInit()
const foo = new FooClass();
console.log(foo.func1()) // prints 'test'
DEMO
OK, now I know what I was doing wrong... Actually I had already did this, but not in 'Foo2Service'... Thanks, by the way...
– Harshul Vijay
Mar 28 at 15:27
You are very welcome, glad I could help! :)
– AJT_82
Mar 28 at 18:29
add a comment |
Using Injector should work:
Create the injector:
const injector = Injector.create(
providers: [
provide: Foo2Service, deps:[] ,
]
);
For the sake of the test, let's return the string test from your init function in service:
init()
return 'test';
for testing, in your class you would call init using the injector:
func1()
let myService = injector.get(Foo2Service);
return myService.init();
and finally the component calling func1:
ngOnInit()
const foo = new FooClass();
console.log(foo.func1()) // prints 'test'
DEMO
OK, now I know what I was doing wrong... Actually I had already did this, but not in 'Foo2Service'... Thanks, by the way...
– Harshul Vijay
Mar 28 at 15:27
You are very welcome, glad I could help! :)
– AJT_82
Mar 28 at 18:29
add a comment |
Using Injector should work:
Create the injector:
const injector = Injector.create(
providers: [
provide: Foo2Service, deps:[] ,
]
);
For the sake of the test, let's return the string test from your init function in service:
init()
return 'test';
for testing, in your class you would call init using the injector:
func1()
let myService = injector.get(Foo2Service);
return myService.init();
and finally the component calling func1:
ngOnInit()
const foo = new FooClass();
console.log(foo.func1()) // prints 'test'
DEMO
Using Injector should work:
Create the injector:
const injector = Injector.create(
providers: [
provide: Foo2Service, deps:[] ,
]
);
For the sake of the test, let's return the string test from your init function in service:
init()
return 'test';
for testing, in your class you would call init using the injector:
func1()
let myService = injector.get(Foo2Service);
return myService.init();
and finally the component calling func1:
ngOnInit()
const foo = new FooClass();
console.log(foo.func1()) // prints 'test'
DEMO
answered Mar 27 at 20:20
AJT_82AJT_82
42k15 gold badges80 silver badges114 bronze badges
42k15 gold badges80 silver badges114 bronze badges
OK, now I know what I was doing wrong... Actually I had already did this, but not in 'Foo2Service'... Thanks, by the way...
– Harshul Vijay
Mar 28 at 15:27
You are very welcome, glad I could help! :)
– AJT_82
Mar 28 at 18:29
add a comment |
OK, now I know what I was doing wrong... Actually I had already did this, but not in 'Foo2Service'... Thanks, by the way...
– Harshul Vijay
Mar 28 at 15:27
You are very welcome, glad I could help! :)
– AJT_82
Mar 28 at 18:29
OK, now I know what I was doing wrong... Actually I had already did this, but not in 'Foo2Service'... Thanks, by the way...
– Harshul Vijay
Mar 28 at 15:27
OK, now I know what I was doing wrong... Actually I had already did this, but not in 'Foo2Service'... Thanks, by the way...
– Harshul Vijay
Mar 28 at 15:27
You are very welcome, glad I could help! :)
– AJT_82
Mar 28 at 18:29
You are very welcome, glad I could help! :)
– AJT_82
Mar 28 at 18:29
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382379%2fusing-services-in-non-angular-classes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where is this
barinstance coming from? (I am referring to codenew FooClass(bar);)– Igor
Mar 27 at 20:21
@Igor Oh, sorry! It was intended to be a
stringor anobject. Fixed.– Harshul Vijay
Mar 28 at 15:28