我在显示异步初始化的变量时遇到了麻烦。我在模板中添加了 ?
以捕获未定义但表示表达式结束意外的错误。
@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>`
})
关于http - 解析器错误 : Unexpected end of expression: {{value? }} | Angular 2 typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071984/