【问题标题】:AngularJS - Displays html tags, how to make it readableAngularJS - 显示 html 标签,如何使其可读
【发布时间】:2016-12-19 06:16:09
【问题描述】:

我使用 PHP 作为 Modal,Angularjs 作为 Controller,为了在前端显示,我将 PHP 转换为 JSON。它在前端显示如下所示。我只想显示一个带有 li 标签和 break 标签以及其他文本的可读文件。我不希望前端有任何其他 html 标签。请帮忙。

前端:

JSON 代码:

PHP 代码:

elseif (isset($_GET['product_id']) && $_GET['product_id'] == 'getProduct') {
    $data = array();

    while ($product = mysqli_fetch_array($query2, MYSQLI_ASSOC)) {

        $name = str_replace('"', "", $product['product_name']);
        $description = str_replace('"', "", $product['product_description']);
        $category_name = str_replace('"', "", $product['category_name']);

        $pro = array('id' => $product['product_id'], 'name' => utf8_encode($name), 'description' => utf8_encode($description),
            'price' => utf8_encode($product['product_price']), 'weight' => $product['product_weight'], 'weight_class' => $product['product_weight_class'], 'quantity' => $product['quantity'],
            'link' => $product['product_image_url'], 'cat' => utf8_encode($category_name));
        array_push($data, $pro);

    }

    $product_encode = json_encode($data);
    //$product_encode = htmlspecialchars_decode($product_encode);
    //$product_encode = html_entity_decode($product_encode);

    echo strip_tags($product_encode);
}

控制器角度:

   $http.get("https://example.com/store/www/api.php?product_id=getProduct")
                    .success(function (detail) {
                        $scope.details = detail;

                        var url = window.location.href;
//$scope.categoryId = '20';
                        var final = url.substr(url.lastIndexOf('/'));
                        $scope.productId = final.split("=")[1];


                        console.log($scope.details);
                    });

查看:

【问题讨论】:

  • 使用 ng-bind-html 指令而不是插值。 ng-bind-html 不会转义 html 实体

    。您需要在项目中包含 angular-sanitize.js 库 - 并设置对 ngSanitize 模块的依赖以使其正常工作。

  • 您可以使用 ng-sanitize 请检查此以供参考embed.plnkr.co/X2g8jE

标签: php angularjs json angularjs-controller ng-bind-html


【解决方案1】:

尝试使用 pre 标签 - 你可以在这里测试 - fiddle

<pre> {{product.description}} </pre>

【讨论】:

  • 我得到相同的输出
【解决方案2】:

使用 Angular ng-sanitize 在视图中格式化 html 页面。例如,请参阅下面的链接。告诉我它是否对你有帮助。

https://docs.angularjs.org/api/ngSanitize/service/$sanitize

【讨论】:

  • 希望得到比直接参考更详细的答案!
  • 我觉得不用多说,那个链接就说明一切了。
  • 这最好是评论!请查看this link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-24
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
相关资源
最近更新 更多