1

I am building a file uploader in Angular4. I need to create a custom object of a file, which has some fields and methods inside, every time a user is dropping/selecting a file.

class File {
  public isSent: boolean: false;
  public send() {
    //a http call here
  }

  constructor(
    private http: Http
  )
}

The problem is that in component, services are created as singletons so there would be only one instance of file per component. I need multiple instances created dynamically.

I can do something like:

let file = new File();

however DI doesn't seem to work here properly as I need to use Http module inside the file instance.

how to manage such situation?

What I basically need is a simple Class implementation that is using some Angular features (Http) basing on which I would create objects. Then, in view, I could just bind file.send() to a button.

Just to clarify that it's not a duplicate: I don't want to inject multiple instances by DI in constructor. I want to create it dynamically on user action.

Tukkan
  • 1,574
  • 2
  • 18
  • 33
  • If this doesn't answer your question leave a comment. My "powers" ;-) immediately close a question when I vote for duplicate, with lower reps, it needs 5 votes to be actually closed ... – Günter Zöchbauer Sep 18 '17 at 15:21
  • @GünterZöchbauer I saw this thread and actually it's a different case. The topic you provided injects instances of a services on initialisation. I need to create it dynamically on user action. I don't know how many instances I need on component creation so I can't inject it it that way in constructor. I added a clarification to the description. – Tukkan Sep 18 '17 at 15:24
  • No, it injects a function, that, when you call it, every time returns a new instance (`editorServiceFactory(1);`) – Günter Zöchbauer Sep 18 '17 at 15:29
  • You may be right. I'll check this out. Thanks – Tukkan Sep 18 '17 at 15:41

0 Answers0