我在显示异步初始化的变量时遇到了麻烦。我?
在模板中添加,以捕获错误,该错误将是未定义的,但它表示表达式结尾意外。
@Component({
template: `<div>{{value?}}</div>`
})
export class Component implements OnInit {
value: number;
constructor(private _service: Service) { }
getValue() {
this._service.getValue().subscribe(data => this.value = data);
}
ngOnInit() {
this.getValue();
}
}
我认为在您的情况下,您可以直接使用变量而无需Elvis运算符:
@Component({
template: `<div>{{value}}</div>`
})
当您尝试访问异步获取的变量的属性时,这很有用。例如:
@Component({
template: `<div>{{value?.someProperty}}</div>`
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句