I have a component which needs to listen to a property changes (timeRemaining) available in a model. When this variable reaches 0, the component triggers an action (for instance, router.navigate(['/my/path']) )
For this purpose, my component subscribes to a Subject, which is notified whenever the value of the property changes. This is working perfectly.
My question is quite simple : where should this Subject be declared? In my model or in the service handling my model?
Example :
In my service :
onTimeExpired(model, callback){
model.getTimeExpiredSubject().subscribe(
() => {
// some logic here
callback();
}
}
OR
In my model
onTimeExpired(callback){
this.getTimeExpiredSubject().subscribe(
() => {
// some logic here
callback();
}
}
Which one is better and why? Thank you.
why you are passing a callback :
//service
onTimeExpired(){
return model.getTimeExpiredSubject();
}
subscription must not be in the service, because the service may be consumed in many components so you have to return an observable.
you have to subscribe in a consumer like a component
// in the component
this.service.subsucribe(data =>{
// do something
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.