当这个值在Subject订阅中被改变时,NgIf不会被触发。控制台输出responseState所需的正确值.responseStateSubject是通过其他分量的输入得到的。我已经用NgOnChanges实现了它,但这对我来说太不受控制了。
组件代码:
@Input() responseStateSubject: Subject<RequestResponseState>;
ngOnInit(): void {
this.responseStateSubject.subscribe((x) => {
//**** This is the relevant part, it does set the property and prints the corresponding value. *//
this.responseState = x as RequestResponseState;
console.log(this.responseState);
//****//
if (x != undefined && x != null) {
this.mainWrapper.nativeElement.classList.add("open-main-wrapper");
if (x.requestEnum != this.requestResultBaseEnum.Loading) {
this.utilsS.scrollElementIntoView("response-main-wrapper");
}
} else {
if (this.mainWrapper != null) {
this.mainWrapper.nativeElement.classList.remove(
"open-main-wrapper"
);
}
}
});
}
html组件:
<div #mainWrapper id="response-main-wrapper" class="flex-center">
<div
*ngIf="responseState != null && responseState != undefined"
id="message-main-container"
>
<hr class="grey-hr" />
</div>
</div>
usingChangeDetectorRef的detectChanges ()方法帮我解决了。