Skip to main content
added 146 characters in body
Source Link
Eliseo
  • 58.8k
  • 4
  • 39
  • 82

You can create an Observable of the service and subscribe in home.ts

Your service

//create a Subject
private percentSource:Subject<any>=new Subject<any>();
//create a Observable
percentEvent:Observable<any>=this.percentSource.asObservable();
...
calculateProgress() {
  if (this.showProgressbar = true) {
    ...
    //send a change of observable with next
    this.percentSource.next(percentage);  //return as result the percent
    ...
  } else {
     ...
  }
}

then, in your home you can subscribe to the observable in the function tiggerEven or in progressBar.component in the ngOnInit function

triggerEvent(){
    this.service.showProgressbar = true;
    //takeWhile make you unsubscribe if condition is not successfully
    //NOT put if you subscribe in your progressBar.component
    this.service.percentEvent
    .takeWhile(() =>this.progress!=100)
    .subscribe(result=>  
    {
         this.progress=result;
    } 
}

You can create an Observable of the service and subscribe in home.ts

Your service

//create a Subject
private percentSource:Subject<any>=new Subject<any>();
//create a Observable
percentEvent:Observable<any>=this.percentSource.asObservable();
...
calculateProgress() {
  if (this.showProgressbar = true) {
    ...
    //send a change of observable with next
    this.percentSource.next(percentage);  //return as result the percent
    ...
  } else {
     ...
  }
}

then, in your home you can subscribe to the observable

triggerEvent(){
    this.service.showProgressbar = true;
    //takeWhile make you unsubscribe if condition is not successfully
    this.service.percentEvent
    .takeWhile(() =>this.progress!=100)
    .subscribe(result=>  
    {
         this.progress=result;
    } 
}

You can create an Observable of the service and subscribe in home.ts

Your service

//create a Subject
private percentSource:Subject<any>=new Subject<any>();
//create a Observable
percentEvent:Observable<any>=this.percentSource.asObservable();
...
calculateProgress() {
  if (this.showProgressbar = true) {
    ...
    //send a change of observable with next
    this.percentSource.next(percentage);  //return as result the percent
    ...
  } else {
     ...
  }
}

then, in your home you can subscribe to the observable in the function tiggerEven or in progressBar.component in the ngOnInit function

triggerEvent(){
    this.service.showProgressbar = true;
    //takeWhile make you unsubscribe if condition is not successfully
    //NOT put if you subscribe in your progressBar.component
    this.service.percentEvent
    .takeWhile(() =>this.progress!=100)
    .subscribe(result=>  
    {
         this.progress=result;
    } 
}
Source Link
Eliseo
  • 58.8k
  • 4
  • 39
  • 82

You can create an Observable of the service and subscribe in home.ts

Your service

//create a Subject
private percentSource:Subject<any>=new Subject<any>();
//create a Observable
percentEvent:Observable<any>=this.percentSource.asObservable();
...
calculateProgress() {
  if (this.showProgressbar = true) {
    ...
    //send a change of observable with next
    this.percentSource.next(percentage);  //return as result the percent
    ...
  } else {
     ...
  }
}

then, in your home you can subscribe to the observable

triggerEvent(){
    this.service.showProgressbar = true;
    //takeWhile make you unsubscribe if condition is not successfully
    this.service.percentEvent
    .takeWhile(() =>this.progress!=100)
    .subscribe(result=>  
    {
         this.progress=result;
    } 
}