【发布时间】: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