首页 > 解决方案 > 数据绑定导致 [object Object]

问题描述

我正在尝试将行记录绑定到我的视图。这是我在后端的代码:

 [Route("GetCarCount")]
        [HttpGet]
        public async Task<long> Count()
        {
            return await _context.Cars.CountAsync();
        }

我已经对此进行了测试,并且可以正常工作。我得到一个像数字一样的结果

9

现在我想在视图上绑定它,这是我在服务中的代码。

 getCount() {
    return this.http.get(this.baseUrl + 'GetCarCount');
  }

我这样称呼这个方法:

 GetCarCount() {
    this.countrecords = this.carservice.getCount();
  }

我已经声明了一个这样的变量:

countrecords: any;

现在我像这样绑定它:

<label>Records count: = {{countrecords}}</label>

它不显示数字9而是我得到[object Object]

我试过这样但它对我不起作用

JSON.parse JSON.stringify

我究竟做错了什么?

标签: html.netangulartypescript.net-core

解决方案


从你的实现来看,countrecords将是 type Observable<number>。使用async模板中的管道来解开它的值。

<label>Records count: = {{countrecords | async}}</label>

推荐阅读