I have a component that displays a message for a few seconds, my problem is that I can not subscribe to the service and perform the necessary operations, how to do this?
This was a short example, I want to call the message component at all stages of the project, how do I do that?
homeComponent.ts:
export class HomeComponent {
constractor(privat _service:IHomeService){}
getMessage(){
this._service.get();
}
}
homeComponent.html:
<p (click)="getMessage()">
show Message
</p>
homeService.ts:
@Injectable()
export class HomeService {
openMessage=new Subject<string>();
get() {
setTimeout(()=>{
this.openMessage.next("test")
},1000)
}
}
messageComponent.ts:
export class MessageComponent {
constructor(private _service:HomeService){
this._service.openMessage.subscribe((res)=>{
this.name=res;
this.flag=true;
setTimeout(()=>{
this.flag=false;
},4000)
})
}
name = '';
flag:boolean=false
}
messageComponent.html:
<p [ngStyle]="flag?'display:block':'display:none'">
{{name}}
</p>
I think this does not happen because the message component is not loaded How do I load it in the service?