簡體   English   中英

如何解析嵌入在JavaScript對象內部的字符串中的html?

[英]How to parse the html which is embedded in a string inside a JavaScript object?

JsFiddle

<script>
  angular.module('module', []);

  angular.module('module').controller('hello', function($scope) {
    $scope.obj = {
      "text": "<u>HelloWorld</u>"
    };

  });

</script>

<body>

  <div ng-app='module' ng-controller='hello'>
    Current:{{obj.text}}

    <br>
    <br> Expected:<u>HelloWorld</u>
  </div>
</body>

我試圖讀取存儲在JSON中的對象,然后將其打印在我的網頁上。

我提供了到上面代碼的鏈接。

我期望輸出是下划線的字符串“ HelloWorld”。

ps:

  • 我無法編輯JSON
  • obj是正在獲取的對象,我無法共享JSON,所以我使用了硬編碼值。

您可以出於這種目的使用ng-bind-htmlng-bind-html-unsafe 您必須從angular-sanitize包括ngSanitize

<p ng-bind-html="obj.text"></p>

此處顯示示例

您只想使用如下正則表達式:

$scope.obj.text =  $scope.obj.text.replace(/(<([^>]+)>)/ig,"");

在這里工作

您需要使用angular-sanitize模塊

<script src="path/to/installed/angular-sanitize/angular-sanitize.js"></script>

<script>
    angular.module("module", ["ngSanitize"]);

    angular.module('module').controller('hello', function($scope) {
        $scope.obj = {
            "text": "<u>HelloWorld</u>"
        };

    });

</script>

和你的HTML:

<div ng-app='module' ng-controller='hello'>
    Current: <p ng-bind-html="obj.text"></p>

    <br>
    <br> Expected:<u>HelloWorld</u>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM