我从承诺中获取数据,并将其分配给服务中的对象。在一个组件中,我正在NgOninit上检索该对象并将其分配给一个observable。我的模板有异步管道,但是当我记录它打印出来的值时,从组件中可以正常显示,但是在模板中它显示为空?
private legal = new Subject();
public legal$ = Observable.from(this.legal);
ngOnInit(): void {
this.title.setTitle('Legal Agreement');
this.submitted = this._stepService.submitted;
this.checkBox.nativeElement.checked = this.submitted;
let legal = this._legalService.legalDocument;
// Subscribe to the local provider error service
this.errors$ = this._errorService.error$;
this.errors$.subscribe(value => console.log('Error is --> ', value));
this.legal$.subscribe((value: any) => console.log("legal is: ",value));
// Set the Errors and Responses to the subjects accordingly for broadcasting
this.legal.next(legal);
this.test = this._legalService.legalDocument.legal;
}
HTML
<div class="col-md-12" *ngIf="(legal$ | async); else other_content">
<html-outlet [html]='legal$.legal'></html-outlet>
</div>
<ng-template #other_content>other content here...</ng-template>
答案 0 :(得分:1)
<强>解决方案强>
private legal: BehaviorSubject<any> = new BehaviorSubject<any>(null);
要检查它是否有效Click here
BehaviourSubject将在Subscription
上返回初始值或当前值主题没有任何值,只返回订阅的当前值。
答案 1 :(得分:0)
试试这个,
super('AUSTRALIA')