1

test.html

<html>
<body ng-app="myApp" ng-controller="myCtrl">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

  <p>Response from process.php is : {{message}}</p>

  <script>
    var app = angular.module("myApp", []);
    app.controller("myCtrl", function($scope, $http){
      $http({
        method : "post",
        url : "process.php"
      })
      .then(function(response){
        // First function handle success
        $scope.message = response.data;
      }, function(response){
        // Second function handle error
        $scope.message = response.status +' ('+ response.statusText + ' )';
      })
    });
  </script>
</body>
</html>

process.php

<?php
    $data = "<h1>Hey, how are you...???</h1>";
    echo $data;
?>

Response from process.html is : <h1>Hey, how are you...???</h1>

You can see that the output is not as expected. It puts the h1 element in the body. But the output should be an heading. How can I do this..?? Any suggestions please.

SOLVED

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

    <script>
        var app = angular.module("myApp", []);
        app.controller("myCtrl", function($scope, $http, $sce){
           $http({
                method : "post",
                url : "process.php"
              })
              .then(function(response){
                 // First function handle success
                 $scope.message = response.data;
                 $scope.messageHtml = $sce.trustAsHtml($scope.message)
               }, function(response){
                  // Second function handle error
                  $scope.messageHtml = response.status +' ('+ response.statusText + ' )';
              })
          });
    </script>
1

5 Answers 5

2

Found solution

I have to use $sce with ng-bind-html .

HTML

<p ng-bind-html="messageHtml"></p>

Controller

$scope.message = response.data;
$scope.messageHtml = $sce.trustAsHtml($scope.message)
Sign up to request clarification or add additional context in comments.

Comments

1

you can use ng-bind-html like this

<p ng-bind-html="message" ></p>

1 Comment

Tried, but No output
1

Check This code :

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
    var app = angular.module("myApp", []);
    app.controller("myCtrl", function($scope, $http,$sce){
      $http({
        method : "post",
        url : "process.php"
      })
      .then(function(response){
        // First function handle success
        $scope.message = $sce.trustAsHtml(response.data);
      }, function(response){
        // Second function handle error
        $scope.message = response.status +' ('+ response.statusText + ' )';
      })
    });
  </script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
  <p>Response from process.php is : <span ng-bind-html="message"></span></p>
</body>
</html>

2 Comments

Yes this is correct, I already answered this code above. Thanks bdw
More important is, your problem fixed, Thanks
0

You have to use ng-bind-html directive.

Ref: https://docs.angularjs.org/api/ng/directive/ngBindHtml

<p>Response from process.php is : </p><div ng-bind-html="message"></div>

2 Comments

Tried, but No output
What error, are you getting..$sanitize service must be loaded
0

I have created a directive for just this scenario. You need to have angular-sanitize included in your project.

(function () {
    'use strict';

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

Then in your HTML, you can write:

<p>{{message | to_trusted}}</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.