Instance of new object

Hi everyone, I have this class:

@Injectable()
export class Utente {
    nome: string;
    descrizione: string;

  constructor(
    public storage: Storage
  ) { }
}

I’d like to create new Utente without specify storage, how can I do it (without removing storage)?
Now I can’t do something like this because the constructor requires storage:

let user = new Utente();
      user.nome = "prova1";
      user.descrizione = "prova2";

I’m guessing you want this?

@Injectable()
export class Utente {
  nome: string;
  descrizione: string;

  constructor(public storage?: Storage) { }
}

Kind of hard to know based on your description

Isn’t that a bad practice to try to create a new local object from a singleton object provider?

1 Like

Lol, very good point. Yes, with a general class my solution is what you want, but once you add the @Injectable() on there, the assumption is this would be a singleton, so you’d never new it up anyway.

Good call.

1 Like

Thank you, it works.

Why is it a bad practice?

Providers are singleton respectively are loaded once for the all runtime life (start till stop) of your app.