How to instanciate class which need Dependancy Injection



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I m using Angular 6.1.1 and I've a class model which need an injected service variable.
Is there any way to instanciate my class without providing any DI ?



Maybe, i'm doing that in the wrong way, how to well used angular with this kind of use case.



Thanks in advance



The goal is to achieve this works with well injectedService in the class



 @Injectable(
providedIn: 'root'
)

export class AuthenticationService
public User : User = new User();
constructor()


export class Item
public UserCreator: User;
// Other properties
constructor(private authenticationService : AuthenticationService)
this.UserCreator = this.authenticationService.User;



export class ExampleComponent
constructor()
const newItem : Item = new Item();
//Doesn't compiled because need authenticationService argument
//Is there any way to ask Angular to instanciate this class with auto resolving the injection of the authenticationService ?











share|improve this question
























  • If it's an item that you will create, why does it have @Injectable ? it should be on service ?

    – Alann
    Nov 15 '18 at 10:05











  • to make the injection works for the example of course, i want to remove it

    – rdecla
    Nov 15 '18 at 10:09











  • providedIn: 'root' means it is provided in the root module, making it a singleton. Why would you create a new instance of that service ?

    – trichetriche
    Nov 15 '18 at 10:17











  • I recreate my example to be more comprehensible

    – rdecla
    Nov 15 '18 at 10:45











  • create a stackblitz to show us what you want to do

    – Alann
    Nov 15 '18 at 12:54

















0















I m using Angular 6.1.1 and I've a class model which need an injected service variable.
Is there any way to instanciate my class without providing any DI ?



Maybe, i'm doing that in the wrong way, how to well used angular with this kind of use case.



Thanks in advance



The goal is to achieve this works with well injectedService in the class



 @Injectable(
providedIn: 'root'
)

export class AuthenticationService
public User : User = new User();
constructor()


export class Item
public UserCreator: User;
// Other properties
constructor(private authenticationService : AuthenticationService)
this.UserCreator = this.authenticationService.User;



export class ExampleComponent
constructor()
const newItem : Item = new Item();
//Doesn't compiled because need authenticationService argument
//Is there any way to ask Angular to instanciate this class with auto resolving the injection of the authenticationService ?











share|improve this question
























  • If it's an item that you will create, why does it have @Injectable ? it should be on service ?

    – Alann
    Nov 15 '18 at 10:05











  • to make the injection works for the example of course, i want to remove it

    – rdecla
    Nov 15 '18 at 10:09











  • providedIn: 'root' means it is provided in the root module, making it a singleton. Why would you create a new instance of that service ?

    – trichetriche
    Nov 15 '18 at 10:17











  • I recreate my example to be more comprehensible

    – rdecla
    Nov 15 '18 at 10:45











  • create a stackblitz to show us what you want to do

    – Alann
    Nov 15 '18 at 12:54













0












0








0








I m using Angular 6.1.1 and I've a class model which need an injected service variable.
Is there any way to instanciate my class without providing any DI ?



Maybe, i'm doing that in the wrong way, how to well used angular with this kind of use case.



Thanks in advance



The goal is to achieve this works with well injectedService in the class



 @Injectable(
providedIn: 'root'
)

export class AuthenticationService
public User : User = new User();
constructor()


export class Item
public UserCreator: User;
// Other properties
constructor(private authenticationService : AuthenticationService)
this.UserCreator = this.authenticationService.User;



export class ExampleComponent
constructor()
const newItem : Item = new Item();
//Doesn't compiled because need authenticationService argument
//Is there any way to ask Angular to instanciate this class with auto resolving the injection of the authenticationService ?











share|improve this question
















I m using Angular 6.1.1 and I've a class model which need an injected service variable.
Is there any way to instanciate my class without providing any DI ?



Maybe, i'm doing that in the wrong way, how to well used angular with this kind of use case.



Thanks in advance



The goal is to achieve this works with well injectedService in the class



 @Injectable(
providedIn: 'root'
)

export class AuthenticationService
public User : User = new User();
constructor()


export class Item
public UserCreator: User;
// Other properties
constructor(private authenticationService : AuthenticationService)
this.UserCreator = this.authenticationService.User;



export class ExampleComponent
constructor()
const newItem : Item = new Item();
//Doesn't compiled because need authenticationService argument
//Is there any way to ask Angular to instanciate this class with auto resolving the injection of the authenticationService ?








angular dependency-injection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 10:43







rdecla

















asked Nov 15 '18 at 10:00









rdeclardecla

6017




6017












  • If it's an item that you will create, why does it have @Injectable ? it should be on service ?

    – Alann
    Nov 15 '18 at 10:05











  • to make the injection works for the example of course, i want to remove it

    – rdecla
    Nov 15 '18 at 10:09











  • providedIn: 'root' means it is provided in the root module, making it a singleton. Why would you create a new instance of that service ?

    – trichetriche
    Nov 15 '18 at 10:17











  • I recreate my example to be more comprehensible

    – rdecla
    Nov 15 '18 at 10:45











  • create a stackblitz to show us what you want to do

    – Alann
    Nov 15 '18 at 12:54

















  • If it's an item that you will create, why does it have @Injectable ? it should be on service ?

    – Alann
    Nov 15 '18 at 10:05











  • to make the injection works for the example of course, i want to remove it

    – rdecla
    Nov 15 '18 at 10:09











  • providedIn: 'root' means it is provided in the root module, making it a singleton. Why would you create a new instance of that service ?

    – trichetriche
    Nov 15 '18 at 10:17











  • I recreate my example to be more comprehensible

    – rdecla
    Nov 15 '18 at 10:45











  • create a stackblitz to show us what you want to do

    – Alann
    Nov 15 '18 at 12:54
















If it's an item that you will create, why does it have @Injectable ? it should be on service ?

– Alann
Nov 15 '18 at 10:05





If it's an item that you will create, why does it have @Injectable ? it should be on service ?

– Alann
Nov 15 '18 at 10:05













to make the injection works for the example of course, i want to remove it

– rdecla
Nov 15 '18 at 10:09





to make the injection works for the example of course, i want to remove it

– rdecla
Nov 15 '18 at 10:09













providedIn: 'root' means it is provided in the root module, making it a singleton. Why would you create a new instance of that service ?

– trichetriche
Nov 15 '18 at 10:17





providedIn: 'root' means it is provided in the root module, making it a singleton. Why would you create a new instance of that service ?

– trichetriche
Nov 15 '18 at 10:17













I recreate my example to be more comprehensible

– rdecla
Nov 15 '18 at 10:45





I recreate my example to be more comprehensible

– rdecla
Nov 15 '18 at 10:45













create a stackblitz to show us what you want to do

– Alann
Nov 15 '18 at 12:54





create a stackblitz to show us what you want to do

– Alann
Nov 15 '18 at 12:54












0






active

oldest

votes












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%2f53316813%2fhow-to-instanciate-class-which-need-dependancy-injection%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f53316813%2fhow-to-instanciate-class-which-need-dependancy-injection%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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo