http - 解析器错误 : Unexpected end of expression: {{value? }} | Angular 2 typescript

标签 http typescript angular

我在显示异步初始化的变量时遇到了麻烦。我在模板中添加了 ? 以捕获未定义但表示表达式结束意外的错误。

@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/

相关文章:

Angular 2 请求的资源上不存在 'Access-Control-Allow-Origin' header

silverlight - 随后启动两个 WebClient.UploadStringAsync 调用

javascript - 如何使用 Angular/Typescript 获取波形音乐播放器

validation - Angular2 自定义表单验证器在类中失去对 `this` 的访问权限

使用 Visual Studio 2015 无法在 Angular 2 上编译 Typescript 文件

http - 为什么 rev 规范链接总是使用 302 重定向

angularjs - 在 Node 应用程序中使用 Angular $http 服务

javascript - TypeError : self. parent.parent.context.parseInt 不是函数

javascript - 为 getter 提供默认值,而不会导致 Angular 2 模型类中的堆栈溢出

angular - 如何观察ng-content中的input元素变化