【问题标题】:Text not wrapping in p tag from Json text file.(Angular Js) [duplicate]文本未包含在 Json 文本文件中的 p 标签中。(Angular Js)[重复]
【发布时间】:2016-05-18 01:24:51
【问题描述】:

我正在从 JSON 中检索数据并检索一些文本内容,但文本内容不支持 p 标签。我将向您展示屏幕截图以获取提示。

当我们检索到文本内容时,它会像这样显示(带有 p 标签)。 JSON 网址:

http://www.json-generator.com/api/json/get/bTRWzhCvuG?indent=2

HTML:

<div ng-controller="chooseProductDescription">
      <div ng-repeat="cat in productDescription">
                <div>{{cat.product.fullDescription}} </div>             
       </div>
</div>

控制器:

.controller('chooseProductDescription', function($scope, $http){
    $http({
        method:'get',
        url:'http://www.json-generator.com/api/json/get/bTRWzhCvuG?indent=2',
        header:{'Content-Type':'application/json'},
    }).success(function(data,status,headers, config){   
        $scope.productDescription = data;       
    }).error(function(data, status,headers, config){

    })
})

在查看页面中检索内容时,角度 js 中不支持 p 标签。我该怎么办?请告诉我。提前致谢。

【问题讨论】:

  • 我会假设cat.product.fullDescription 不会将fullDescription 数据输出为原始html,而是将其视为浏览器输出,例如对html 进行编码。
  • 尝试在您的success $scope.productDescription = $sce.trustAsHtml(data) 中像这样使用$sce
  • @Sachin。这是行不通的。多解释一下?
  • 您是否尝试实现 ngBindHtml ?正如答案中所建议的那样?
  • @Saching。是的,但我没有安装 sanitize。 ngBindHtml 可以在没有消毒的情况下工作吗?

标签: javascript html angularjs json ajax


【解决方案1】:

使用ngBindHtml

<div ng-controller="chooseProductDescription">
      <div ng-repeat="cat in productDescription">
                <div ng-bind-html="cat.product.fullDescription"></div>             
       </div>
</div>

【讨论】:

    猜你喜欢
    • 2013-01-06
    • 1970-01-01
    • 2011-01-09
    • 2017-02-02
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多