In my application I use behavior subject to load data :
My service :
data$ = new BehaviorSubject({
users: [],
user: {}
});
constructor();
}
dispatch(action: Action): Observable<true | { error: string; }> {
switch (action.type) {
case ActionTypes.USER_LIST:
this.getUsers();
return of(true);
default:
return of(true);
}
}
private getUsers(){
this.http.get("url")
.subscribe((users: any) => {
this.data$.next({...this.data$.value, users: users});
});
}
I call this service in my parent component when my child component asks for it.
MyParentComponent :
export class MyParentComponent implements OnInit {
isLoading = false;
user$ = new BehaviorSubject(null);
constructor(private userService: UserService) { }
ngOnInit(): void {
}
getUsers(event) {
this.isLoading = true;
this.userService.dispatch({type: Action Types.USERR_LIST});
this.user$.next(this.userService.data$);
this.isLoading = false;
}
}
MyChildComponent : call load data :
private getData() {
this.onDataChange.emit(this.criteria);
this.dataSource.data = this.items;
console.log(this.dataSource);
this.isLoading = false;
}
Call my child component :
<app-child-comp
[entity]="(user$| async)?.value?.userList"
(onDataChange)="getUsers($event)"
class="row col-md-12"
>
</app-child-comp>
I don’t understand why my datasource or my items attribute does not update after onEmit whereas when I display the items attribute to json training in my child component it is up to date