Angular 在角度8中显示空值的可观察属性
我有以下组件Angular 在角度8中显示空值的可观察属性,angular,angular8,Angular,Angular8,我有以下组件 import { Component, OnInit } from '@angular/core'; import { DataService } from '../Services/data-service'; import { Observable } from 'rxjs'; import { ShowItem } from '../Models/show-item'; @Component({ selector: 'app-show', templateUrl:
import { Component, OnInit } from '@angular/core';
import { DataService } from '../Services/data-service';
import { Observable } from 'rxjs';
import { ShowItem } from '../Models/show-item';
@Component({
selector: 'app-show',
templateUrl: './show.component.html',
styleUrls: ['./show.component.scss']
})
export class ShowComponent implements OnInit {
public showString:Observable<ShowItem>;
constructor(public datasource:DataService) { }
ngOnInit() {
this.getShow();
}
getShow()
{
this.showString = this.datasource.getShow();
this.showString.subscribe(m=>{console.log(m);});
}
}
为什么对于明显存在的属性,我的值会变为null?在
async
解析后,您需要访问imageUrl
属性
展示作品
{(showString | async)?.imageUrl}
请注意安全导航器操作员(?。
);这将避免在async
调用解决之前尝试访问imageUrl
属性时模板上出现任何错误
由于
imageUrl
属性是字符串,因此|json
管道也被删除,因为该属性位于结果上,而不是可观察的:(showString | async)。imageUrl
。
<p>show works!</p>
<p> {{showString|async|json}}</p>
<p>show works!</p>
<p>{{showString.imageUrl|async|json}}</p>
export class ShowItem {
public imageUrl:string;
public showId:string;
}