0

I am new to angular js. So this might be very basic question

I have external API data which is a user generated content. The client wants to dynamically show the content. In content, there is script in which directive is created, I tried using ng-bind-html but it doesn't work.

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

want to execute the script in which directive is created and same directive should be injected in html content.

var data = '<script> var app = angular.module(\'main\', []);' +
'app.directive(\'slideImageComparison\', function () {return { restrict:           \'E\', scope: { imageInfo: \'=info\'}, link: function (scope, elem, attr) { console.log(\'directive called\');' +
    '},template: \'<div class="slide-comb"> test </div>\'' +
'}; }); </script>      <slide-image-comparison></slide-image-comparison>';

$scope.myHTML= $sce.trustAsHtml(data)

I added backslash to escape a single quote.

help is appreciated here.

6
  • This won't work, due to browser design. <script> tags inserted into HTML are executed on page load; since this content doesn't exist when the page is loaded, it will never be executed. You should look into angular lazy loading techniques instead. Commented Mar 12, 2017 at 15:13
  • Also, this script would break your main module if it executed anyway, since it appears to re-declare the main module, instead of adding to it. Commented Mar 12, 2017 at 15:14
  • Thanks, is there any other way to do the same? Commented Mar 12, 2017 at 15:27
  • @user3106005 how about using regexp to get the script and eval it? Commented Mar 12, 2017 at 15:28
  • sorry I'm new to this, any example would really appreciated. Commented Mar 12, 2017 at 15:29

1 Answer 1

1

Demo: http://plnkr.co/edit/L8FWxNSQO6VAf5wbJBtF?p=preview

Base on Add directive to module after bootstrap and applying on dynamic content

html:

<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
  <script src="./app.js"></script>
</head>



<body  ng-app="demo">
  <div ng-controller="mainController">
    <external-html external-data="external"></external-html>
  </div>
</body>

</html>

js:

var module1 = angular.module("demo", []);
module1.controller("mainController", ["$scope", function(sp) {

  var external = `<script> 
  module1.lazyDirective('sl', function () {
      return { restrict:'E', 
              scope: { imageInfo: '=info'}, 
              link: function (scope, elem, attr) { 
                  console.log('directive called');
              },
              template: '<div class="slide-comb"> test </div>'}; 
  }); 
  </script>      
  <sl></sl>`;
  sp.external = external;
}]).config(function($compileProvider) {
  module1.lazyDirective = $compileProvider.directive;
}).directive('externalHtml', ["$parse", "$compile", function($parse, $compile) {
  return {
    restrict: 'E',
    link: function(scope, element, attrs) {
      const data = $parse(attrs.externalData)(scope);
      const el = document.createElement('div');
      el.innerHTML = data;
      const scripts = el.getElementsByTagName("script");
      for (let i in scripts) {
        console.log(scripts[i].textContent)
        eval(scripts[i].textContent);
      }

      element.append($compile(data)(scope));


    }
  }
}]);
Sign up to request clarification or add additional context in comments.

15 Comments

Hi, I tried eval but still its not working :(. is there any way to check whether script is loaded/executed correctly?
You can console.log() the scripts. BTW, maybe you should use "$compile" to handle the html string.
are you suggesting this way? $scope.myHtml = $compile(data)($scope);
Nope, u get a element returned by the function, not a html string. You should write a directive for it using "append" things.
@user3106005 wait for an hour or two, I'd try it myself. It's working time in China now.
|

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.