模板中的异步管道不呈现数据

时间:2018-01-17 19:32:40

标签: angular angular5

我从承诺中获取数据,并将其分配给服务中的对象。在一个组件中,我正在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>

2 个答案:

答案 0 :(得分:1)

在这种场景中,我们想要“行为主体”

<强>解决方案

private legal: BehaviorSubject<any> = new BehaviorSubject<any>(null);

要检查它是否有效Click here

行为主题

BehaviourSubject将在Subscription

上返回初始值或当前值

主题

主题没有任何值,只返回订阅的当前值。

Read more

答案 1 :(得分:0)

试试这个,

super('AUSTRALIA')