AngularJS - 显示html标签,如何使其可读

时间:2016-12-19 06:16:09

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

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

前端:

enter image description here

JSON代码:

enter image description here

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);
}

Controller Angular:

   $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);
                    });

查看:

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试使用预标记 - 您可以在此处测试 - fiddle

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

答案 1 :(得分:0)

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

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