每次发出HTTP请求时,我都有HTTP拦截器打开加载栏:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const dataStorageService = this.injector.get(DataStorageService);
dataStorageService.openProgressbar.next(true);
return next
.handle(req)
.do(evt => {
if (evt instanceof HttpResponse) {
dataStorageService.openProgressbar.next(false);
}
})
.catch(err => {
return Observable.throw(err);
});
}
然后在我的数据存储服务中,我有一个主题:
openProgressbar = new Subject<boolean>();
然后我在我的应用程序组件中放置了加载栏:
ngOnInit() {
this.dataStorageService.openProgressbar.subscribe(ifOpen: boolean => {
this.alertMessageNavbarStatus = ifOpen;
});
}
html:
<div id="Intro" *ngIf="alertMessageNavbarStatus">
<app-progress-bar-indeterminate-example>
</app-progress-bar-indeterminate-example>
</div>
但是正在显示ExpressionChangedAfterItHasBeenCheckedError。我一直在寻找使用ngAfterContentChecked的解决方案,但对我而言不起作用。有人可以帮我吗?
答案 0 :(得分:0)
IMO将您的条件逻辑放入setTimeout
技巧中,就像这样-
setTimeout(() => {
this.dataStorageService.openProgressbar.subscribe(ifOpen: boolean => {
this.alertMessageNavbarStatus = ifOpen;
});
},0);
有关更多详细信息,我强烈建议您阅读本文-