显示从MySQL数据库检索的HTML

时间:2017-05-02 00:11:29

标签: html mysql angularjs

所以我从MySQL数据库返回一个字符串,如下所示<i>a</i><sup>2</sup> + <i>b</i><sup>2</sup> = <i>c</i><sup>2</sup>

不幸的是,该字符串在浏览器中显示为<i>a</i><sup>2</sup> + <i>b</i><sup>2</sup> = <i>c</i><sup>2</sup>

PHP文件如下:

<?php
    require_once './config.php';

    $query = $db->prepare("SELECT * FROM `events`");
    $query->execute();

    $results = $query->fetchAll(PDO::FETCH_ASSOC);

    echo $json_response = json_encode($results);

?>

这是使用资源在JS控制器中解析的:

$scope.events = data;

$resource($scope.events).get().$promise.then(function (events) {
                        vm.events = events;
                    });

它的工作原理是通过ngRepeat显示内容,如下所示:

<div class="container" id="visualization" ng-repeat="event in events">

            <div class="row">
                <div ng-class-odd="'col-md-6 col-md-offset-6'" ng-class-even="'col-md-6'">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            {{event.day}} {{event.month}} {{event.year}}
                        </div>

                        <div class="panel-body">
                            {{event.description}}
                        </div>
                    </div>
                </div>
            </div>
        </div>

任何帮助都会非常多,非常感谢!谢谢!

编辑1:

鉴于以下解决方案,我改变的唯一内容是在我的控制器文件和视图文件本身中,即:

控制器文件

var angular_module_name = angular.module('angular_module_name', []);

angular_module_name.filter('to_trusted', ['$sce', function ($sce) {
    return function (text) {
        return $sce.trustAsHtml(text);
    };
}]);

查看文件

    <div class="container" id="visualization" ng-repeat="event in events">

        <div class="row">
            <div ng-class-odd="'col-md-6 col-md-offset-6'" ng-class-even="'col-md-6'">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        {{event.day}} {{event.month}} {{event.year}}
                    </div>

                    <div class="panel-body">
                        <span class="description" ng-bind-html="event.description | to_trusted"></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

添加过滤器:

.filter('to_trusted', ['$sce', function ($sce) {
    return function (text) {
        return $sce.trustAsHtml(text);
};

在html中,

<div class="description" ng-bind-html="description | to_trusted"></div>