K
K
Konstantin2018-10-21 00:43:25
Angular
Konstantin, 2018-10-21 00:43:25

One custom class instance in Angular 2?

I have a custom class like this:

export class SingletonClassRegister {
  public registeredClasses: ClassItem = {};
  public getClass(obj: any) {
      this.registeredClasses['name'] = obj;
  }
}

Which fills the registeredClasses object with ready-made instance objects of other classes.
I registered this class in Angular in app.module:
providers: [SingletonClassRegister]
Then I use it in components:
constructor(
private singletonClassRegister: SingletonClassRegister) {}

When I register an object in one of the components:
this. singletonClassRegister.getClass(new Object());

And then in another component I display the singletonClassRegister object, I see that it is empty there:
console.log(this.singletonClassRegister.registeredClasses);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
Qairat, 2018-10-26
@Junart1

I think it is necessary to do it through the EventEmitter, write the following inside the SingletonClassRegister: then use it in this way:
and in another component accept it:

this.singletonClassRegister.registeredClasses.subscribe(res => {
  console.log(res);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question