I am facing a problem on how to implement an observable correctly in a library (angular) to be an api.
The library displays a div with some text inside. Inside the component of the library, there is a boolean that I use to display the div html element. I want to build an observable based on that boolean to be exposed for the user in order to let them know if the div is displayed or not and do stuff.
Inside project, I created a service that consumes observable on bolean. Inside the component of the div i am importing that service and when there is a boolean true or false i set the value to functions of the services. I am using rxjs ( quiet easy to use)..
private Boolean: BehaviorSubject<boolean>;
export class getDivStatus {
public getTheBoolean(): Observable<boolean> {
return this.theBoolean.asObservable();
}
public setTheBoolean(newValue: boolean): void {
this.theBoolean.next(newValue);
}
}
My component
...
import {getDivStatus } from /getDivStatus.service.ts
...
export class divCom implement oninit {
constructor(private divservice: getDivStatus)
ngOnInit() {
booleanDiv = true;
this.divservice.setTheBoolean(true);
if(anotehrcondition) {booleanDiv = false; this.divservice.setTheBoolean(false);}
}
// Building up the library and implement it on an angular project :
app.compoennt.ts
import {librarytotest} from librarytotest;
@compoennt({
...})
export class AppCompoet implements oninit{
ngoninit() {
this.librarytotest.getboolean().subscribe({x => console.log(x)});
}
}
On another side project, after building up the library, I want to subscribe to those events, but i don't know how i can achieve that. What the better way or the proper manner to achieve do that ?