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;








1















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.










share|improve this question


























  • 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

















1















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.










share|improve this question


























  • 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













1












1








1








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.










share|improve this question
















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.







angular typescript angular-services






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















1















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






share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









1















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






share|improve this answer

























  • 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















1















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






share|improve this answer

























  • 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













1














1










1









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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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








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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해