doww38701 2015-02-22 22:58
浏览 25
已采纳

当使用$ http Angular时,如何将HTML显示为HTML而不是文本

I've been stuck on this for days.

Basically I have a $http.post with Angular, posting an e-mail address and message to be emailed from post.php. Post.php is then echoing text depending on the result of the mail(), but when I return

Success

for example, it will actually show the HTML on the DOM instead of process it.

Appreciate any help.

app.controller('contactsController', function($scope, $rootScope, $http) {

        $rootScope.currentPage = "contact";
        $scope.postData = {};
        $scope.data = "Send";

        $scope.runScript = function() {
            $http({
                url: "post.php",
                method: "POST",
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                data: $.param({"postData":$scope.postData})
            }).success(function(data) {
                $scope.data = data;
                console.log(data);
                $scope.contactForm.$valid = false;
            }).error(function(data, status, headers, config) {
                $scope.status = status;
            });

        };
    });
<?php

// get the raw POST data

$inputData = $_POST['postData'];

if(isset($inputData["email"])&&isset($inputData["message"])) {

    $email = $inputData["email"];
    $message = $inputData["message"];

    $to = "XXXXXXXXXXXXX@XXXXXXXXXX.COM"; // required

    $from = $email; // required

    $body = $message;

    $subject = "Online Query Submission";


// create email headers

    if(mail($to,$subject,$body,"From:$from/r/nReply-to:$from"))
        echo "<p>Success - Thanks!</p>";
    else
        echo "<p>Error - Sorry!</p>";

}

 ?>
  • 写回答

3条回答 默认 最新

  • dongxing4643 2015-02-22 23:03
    关注

    I believe you should use ng-bind-html in your view, like so:

    <div ng-bind-html="data"></div>
    

    as you attribute the message returned from the server to a controller $scope variable, like so:

    $http(url).success(function(data){
    $scope.data=data;
    });
    

    From angularjs doc about ngBindHtml,

    ng-bind-html evaluates the expression and inserts the resulting HTML into the element in a secure way

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。