【问题标题】:How to remove whitespace at the beginning of my json angularjs如何在我的 json angularjs 开头删除空格
【发布时间】:2021-11-30 08:02:20
【问题描述】:

我添加了一个发送给我的 json

{"idCommande":73864,"status":"error"}

我的问题是当我将我的 json 放入时

<code>
    <pre>
        {{ jsonFormatter | json}}
    </pre>
</code>

它会在我的第一个 { 之前和我最后一个 } 之后自动添加到我的 json 空格中。

我已经尝试过JSON.parse(JSON.stringify(response.data).replace(/\s(?=\w+":)/g, ""))

但是还是一样的问题

【问题讨论】:

    标签: json angularjs


    【解决方案1】:

    &lt;pre&gt; 保留空格,包括初始缩进。您可以确保初始表达式没有缩进,如下所示:

    <code>
        <pre>
    {{ jsonData | json }}
        </pre>
    </code>
    

    唯一的问题是它看起来不太好,并且您的 IDE 可能会在您不注意的情况下“修复”缩进。更好的方法是使用ng-bind。它不仅是 more performant best practice,而且还恰好通过不包含前导空格来解决您的问题。

    <code>
        <pre ng-bind="jsonData | json"></pre>
    </code>
    

    附:在我的示例中,我将 jsonFormatter 重命名为 jsonData,因为我真的无法将格式化程序传递给 AngularJS 的 json 过滤器,它本身就是一个格式化程序。考虑为该属性取一个更好的名称。

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多