【问题标题】:Angular 5 Omitting .0 from from 1.0 from an API responseAngular 5 从 API 响应的 1.0 中省略 .0
【发布时间】:2019-04-16 05:46:31
【问题描述】:

在 Angular 5 中,我得到的 API 响应为 1.0。但是在填充到 html 字段时,它显示为 1。

在 chrome 开发工具中,在 Network tab->Response 下显示为 1.0,但在 Network Tab -> Preview 下显示为 1。

我尝试将响应映射到有角度的模型,但没有成功。

 response is : 
    {
        amount: 1.0,
        quantity : 3.0,
        id: "20184563251",
    }


this.result = reportResponse.body;
this.cartForm.patchValue(
    {
        'inputTextField': this.result.amount, // While patching only 1 is patched
     });

【问题讨论】:

    标签: javascript angular typescript api httpresponse


    【解决方案1】:

    试试toFixed。它会返回一个字符串

    this.rateData.amount.toFixed(1)
    

    【讨论】:

    • 如果我使用 toFixed(1),如果金额包含多于 1 位小数,则仅显示一位数字。例如:2.46 显示为 2.5。但我们想显示为 2.46
    • 在通过toFixed之前放置一个条件@
    【解决方案2】:

    把它当作数字,最好先转换成字符串

    this.result = reportResponse.body;
    this.cartForm.patchValue(
        {
            'inputTextField': String( this.rateData.amount), // convert it to string
         });
    

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多