【问题标题】:Space Between Text In Same JSON String同一 JSON 字符串中文本之间的空格
【发布时间】:2017-09-19 21:39:38
【问题描述】:

我自愿为非营利组织编写一个快速的 AngularJS 应用程序。我已经成功设置了 Angular,但我遇到的一个问题源于 JSON。

我有一个包含多个段落的 JSON 对象。因此,我需要将段落分成两行。我做了一些研究,找到了类似问题的一些答案,但由于没有将它们放在上下文中,我不理解它们。我试图查看 JSON 是否内置了强制换行的内容,因为那样可以,但我没有找到。

非常感谢您的帮助!

带有 Angular JS 的 HTML

<div class="bio">
    {{exhibits[whichItem].bio}}
</div>

JSON

[
  {
    "name":"Name goes here",
    "bio":"First long block of text goes here then it needs a break <br /> and the second long block of text is here."
  }
]

【问题讨论】:

标签: html angularjs json


【解决方案1】:

@user5854648 您的后端以这种格式发送 JSON 响应。如果您使用大于 1.2 的 Angular 版本来显示带有 html 标签的文本内容(人们称之为不安全的 html),请使用名为 $sce 的 angularjs 内置服务。为了使其成为可重用组件,创建了一个自定义过滤器。

var demoApp = angular.module('demoApp',[]);
demoApp.filter('trustAsHtml', [
    '$sce',
    function($sce) {
        return function(value) {
            return $sce.trustAsHtml('html', value);
        }
    }
]);

然后在html中进行更改

<div class="bio">
    {{exhibits[whichItem].bio | trustAsHtml}}
</div>

<div class="bio">
        <p ng-bind='exhibits[whichItem].bio | trustAsHtml'></p>
 </div>

【讨论】:

  • 嗨,Snowp,我觉得我很亲密,但我在这里错过了一个阻止它工作的概念。我看到 return $sce.trustAsHtml('html', value);正在将 html 完全打印为字符串。我不确定如何在此处调用显示被调用的相应 JSON 文本的 JSON 文件,即 {{exhibits[whichItem].bio}}。我不知道在你的例子中输入什么来准确地拉出括号内的内容。
  • 你问的是如何显示 json 响应?
  • 我可以显示 JSON,但它显示的是 HTML 标签,而不是 HTML 样式。所以如果我用 包裹一些东西,它就不会显得粗体,它实际上是在文本周围显示标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 2014-08-21
  • 2012-02-07
  • 2023-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多