I need to detect the component change.
First check my code which work.
The problem here is that this is called init and it is unnecessary to call all the time ... you should only call for a change or when its data ( response ) is okay.
ngOnInit() {
this.calendarData();
}
detectChangeUser(){
this.sharedService.selectedUserType$.subscribe(
data => {
if(data === 'changed-view-user-trigger'){
this.calendarData();
this.calendarUsers();
}
},
err => console.log(err)
)
}
I need to detect only when data has a response.
Check also my service.
export class SharedService {
public latestViewSwither: any = null;
selectedUserType$ = new BehaviorSubject<any>(this.latestViewSwither);
training$ = this.selectedUserType$.asObservable();
constructor(
private http: HttpClient
) { }
swithViewChanged(e){
this.latestViewSwither = e;
this.selectedUserType$.next(e);
}
}
only to detect when data has value.
data === 'changed-view-user-trigger' don't worry about this. I send it from another component only a string...this is not important. Only important thing is any hook which detects change... I am also trying with ngAfterViewChecked but my software crashes after this...
ifstatement inside your component. you should check if the response is ok to be sent from your service, otherwise your subscripition will always fire. As a side note, you could use a.pipe(filter(...))to trigger subscription only on present dataswitchViewChangedyou should put an if statementif (!!e)...to call thenextfrom the subject. Also you should make your subject private and your subscribe to theasObservable()property instead.