I would like to create a singleton from below class and use the singleton in other files as MyClass.Instance.SomeStatus
. I checked several questions and answers on StackOverflow, but it isn't clear yet how to create a singleton which requires a service as parameter (DI). Could you explain the correct way of implementing this case? (Do I need to add MyClass
to '@NgModule({
' as well?)
Note: I already had a look at this question, but it doesn't explain the DI service parameter.
@Injectable()
export class MyClass {
public SomeStatus: boolean = false;
constructor(private _myService:MyService) {
}
public DoSomething(): void
{
this._myService.observableEvents.subscribe(result => {
this.SomeStatus = result;
});
}
}
Update 1
@Injectable()
export class AnotherClass{
constructor(private _myService:MyService, private myClass:MyClass) {
}
}
In the html file I use: myClass.SomeStatus
.