I have the following case:
Component @permissions this component has a button that triggers a simple promise and set a value on my other component @MenuComponent
export class Permissions {
constructor(private _menu: MenuComponent, private _api: ApiService) {}
btnAction(){
this._api.getDataID(item.project_name, 'usersbyID', userID).then((data: any) => {
this._menu.checkPerm(JSON.parse(data.permissions).description);
});
}
}
its not important what is returned but it is a simple JSON.
In my other component @MenuComponent
@Component({
// ..another configuration non relevant
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MenuComponent implements OnInit {
menus = []
menuPermisson: any;
menuObservable = new Subject();
constructor(private _cdRef: ChangeDetectorRef) {
this.menuObservable.subscribe(value => {
this.menuPermisson = value;
this.reloadAll();
});
}
reloadAll() {
this.menus.push('someValue');
this._cdRef.markForCheck();
}
checkPerm(data) {
this.menuObservable.next(data);
}
}
So the concept is after the promise sends some value to my subject I need to push someValue to the menu until this point it's ok all data get there, but I need to re-render the view so the "someValue" shows on the view. but it does nothing, when I change the this._cdRef.markForCheck()
to this._cdRef.detectChanges()
and put it on a tryCatch it returns Attempt to use a destroyed view: detectChanges
. I can't find a way to render the menu with someValue.
This both components are at the same level on the ChangeDetection Tree