Demo In my application I am using service to communicate between
import {Injectable} from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { TransferModel } from '../models/transfer';
@Injectable()
export class TransferService{
constructor(){}
private paramSource = new BehaviorSubject(new TransferModel());
getData = this.paramSource.asObservable();
setData(param:TransferModel) { this.paramSource.next(param)}
}
My problem is that when I go to components, it works well but in pages send other pages request also. I mean for example I am in Courses.component
constructor(private transferService:TransferService,private dataService:DataService,sessionService:SessionService,private router:Router) {
this.transferService.getData.subscribe(x=>{
this.transfer=x; if(!this.transfer.ExamId || this.transfer.ExamId<=0){ this.router.navigateByUrl("/home");return;}
this.dataService.Post(URLS.url.coursePage.getCourses,{Id:this.transfer.ExamId}).subscribe(elem=>{
this.courses=elem;
});
});
}
code above gets courses list when I click one course , below function run and
goToSubject(id){
this.transfer.CourseId=id;
this.transferService.setData(this.transfer);
this.router.navigateByUrl("/subjects");
}
goes to subjects component. In subjects component I send request with constructor.
constructor(private transferService:TransferService,private dataService:DataService,sessionService:SessionService,private router:Router) {
this.transferService.getData.subscribe(x=>{
this.transfer=x; if(!this.transfer.ExamId || this.transfer.ExamId<=0){ this.router.navigateByUrl("/home"); }
this.dataService.Post(URLS.url.subjectPage.getSubjects,{ExamId:this.transfer.ExamId,CourseId:this.transfer.CourseId}).subscribe(elem=>{
this.subjects=elem;
});
});
}
but here also another page's request call too such as in image. I need that every page send only one its request.
How can I solve this problem?
