gpt4 book ai didi

javascript - 使用 $http Angular 时如何返回 HTML 以显示为 HTML 而不是文本

转载 作者:行者123 更新时间:2023-11-30 17:02:00 25 4
gpt4 key购买 nike

我已经坚持了好几天了。

基本上我有一个带有 Angular 的 $http.post,发布一个电子邮件地址和要从 post.php 通过电子邮件发送的消息。 Post.php 然后根据 mail() 的结果回显文本,但是当我返回

Success

时,它实际上会在 DOM 上显示 HTML 而不是处理它。

感谢任何帮助。

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>";

}

?>

最佳答案

我相信你应该在你的 View 中使用 ng-bind-html,像这样:

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

当您将从服务器返回的消息归因于 Controller $scope 变量时,如下所示:

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

来自 angularjs doc about ngBindHtml ,

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

关于javascript - 使用 $http Angular 时如何返回 HTML 以显示为 HTML 而不是文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28664379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com