0

I am new to angularjs. In my project i am getting an HTML file as http response. ng-bind-html works with html code. But the angularjs code inside html is not getting compiled. I tried the solutions with $compile ,nothing works. Please suggest a good solution for this.

Lets say we have a sample code like this:

    <!DOCTYPE html>
    <html>
     <head>
    <meta charset="utf-8" />
    <title>TEST</title>
    <script src="angular.js"></script> 
     </head>
     <body ng-app="myapp">
     <div ng-controller="myController">
     <p compile="htmlresponse"></p>
     </div>

 <script>
 var app = angular.module('myapp', []);
 app.controller('myController', function ($rootScope,$sce) {
  $http.get(,,,,).success(function(data){
  $rootScope.htmlResponse=$sce.trustAsHtml(data);
  }
 });

 app.directive('compile', function($compile) {
  // directive factory creates a link function
  return function(scope, element, attrs) {
    scope.$watch(
      function(scope) {
        return scope.$eval(attrs.compile);
      },
      function(htmlResponse) {
        element.html(htmlResponse);
        $compile(element.contents())(scope);
      }
    );
  };
});

 </script> 
</body>
</html>

This works fine if i get only HTML as response. When i get HTML with angularjs(which includes app.module,app.controller,app.directives), only the html part is getting compiled. Please help me with this issue.

Here is my sample response.

<!DOCTYPE html>
 <html>
  <head>
    <meta charset="utf-8" />
    <title>TEST</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular-animate.min.js"></script> 
 </head>
 <body ng-app="myapp">
 <div ng-controller="myController">
   {{name.myname}}
 </div>

 <script>
 var app = angular.module('myapp', []);
 app.controller('myController', function ($scope) {
    $scope.name={};
  $scope.name.myname = "stack";
 });

 </script> 
</body>
</html>
sakthi
  • 74
  • 1
  • 7
  • What do you mean by "it is not getting compiled"? Is the embedded JavaScript executed? You are not event using `ng-bind-html` inside the code sample you have provided. From the question (as it is now) it is not clear to me what you are trying to accomplish. – Martin Apr 22 '15 at 12:06
  • I have tried ng-bind-html also. but ng-bind-html doesn't compile javascript code inside the html. Thats why i am trying with $compile,as many suggested it in stackoverflow. – sakthi Apr 22 '15 at 12:10
  • You HTML response must be an html fragment. HTML and BODY are special tags, if they are out of place they are removed by the browser. Also `ng-app` can only ever be run once. Once Angular is initialize any other ng-app is ignored. What you are returning looks like it should be placed into an iframe, although that means it would not be able to communicate with the other app on the page. – Enzey Apr 22 '15 at 15:19
  • @Enzey thank you so much,, it works :) i have been trying this for last three days,,, – sakthi Apr 23 '15 at 05:50

3 Answers3

2

I tried with iframe,, and it works,,, :) Either we have to save the response in an HTML file and use it like this,,,,,,,

<iframe src="htmlResponse.html"> </iframe>

Or we can directly assign the rootscope variable to it.

<iframe width="100%" height="500px" ng-attr-srcdoc="{{htmlResponse}}"></iframe>
sakthi
  • 74
  • 1
  • 7
1

You should try using same version of angularjs and ngsanitize, that solved the same problem for me!

0

If you need to display raw HTML content to the page view with AngularJS, you can use the $sce service that comes with AngularJS. $sce stands for Strict Contextual Escaping. The service has trustAsHTML method with take some arbitrary text or HTML.

Refer this: http://learnwebtutorials.com/using-angularjs-sce-trustashtml-raw-html

Also this: How do you use $sce.trustAsHtml(string) to replicate ng-bind-html-unsafe in Angular 1.2+

Community
  • 1
  • 1
Reena
  • 1,109
  • 8
  • 16