1

I've this widget code inside directive html template.

<script type="text/javascript">
  _hcwp = window._hcwp || [];
  _hcwp.push({widget:"Stream", widget_id:10862, xid:{{_id}});
  (function() {
  if("HC_LOAD_INIT" in window)return;
  HC_LOAD_INIT = true;
  var lang = (navigator.language || navigator.systemLanguage || navigator.userLanguage || "en").substr(0, 2).toLowerCase();
  var hcc = document.createElement("script"); hcc.type = "text/javascript"; hcc.async = true;
  hcc.src = ("https:" == document.location.protocol ? "https" : "http")+"://w.hypercomments.com/widget/hc/10862/"+lang+"/widget.js";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hcc, s.nextSibling);
  })();

Inside this template have a variable {{_id}}.

But when angular render this template, variable is not sent inside template.

At some point have to send a variable inside temple for render?

My directives:

app.directive('hyperComments', function($timeout) {
return {
    restrict: 'E',
    templateUrl: './templates/hc.html',
    replace: true,
    scope: {
      xid: '='
    },
    link: function(scope, element, attrs) {
    console.log('hello!');
    $timeout(function() {
        _hcwp = window._hcwp || [];
        _hcwp[0]= { widget:"Stream", widget_id: 28172, xid: 31241123};
        // HC.widget("Stream", _hcwp);

        (function() {
          // if("HC_LOAD_INIT" in window)return;
          // var HC_LOAD_INIT = true;
          console.log('in');
          var lang = (navigator.language || navigator.systemLanguage || navigator.userLanguage ||  "en").substr(0, 2).toLowerCase();
          var hcc = document.createElement("script"); hcc.type = "text/javascript"; hcc.async = true;
          hcc.src = ("https:" == document.location.protocol ? "https" : "http")+"://w.hypercomments.com/widget/hc/9345/"+lang+"/widget.js";
          var s = document.getElementsByTagName("script")[0];
          console.log(s);
          s.parentNode.insertBefore(hcc, s.nextSibling);
          })();
    }, 10000);

}
};

});

Template:

< div> < div id="hypercomments_widget"> comments powered by HyperComments < /div>

2
  • can you provide the full code of your template and directive? Commented Apr 12, 2015 at 8:07
  • @AvraamMavridis I provede the full code. Commented Apr 13, 2015 at 20:41

1 Answer 1

2

There is unmatched bracket in the string

_hcwp.push({widget:"Stream", widget_id:10862, xid:{{_id}});

Regardless of that, it is not syntactically correct and will throw an error, Angular doesn't parse the contents of <script>.

Moreover, Angular doesn't handle <script> tag in templates (security concerns or oversimplification of jqlite implementation), so

app.directive('sample', function () {
  return {
    scope: {},
    controller: function ($scope, $window) {
      $scope.bar = 'test';
      $window.foo = $scope.bar
    },
    link: function (scope, element, attrs, ctrl) {
      var scriptInane = element.find("script")[0];
      var script = document.createElement("script");
      script.innerHTML = scriptInane.innerHTML;
      scriptInane.parentNode.replaceChild(script, scriptInane); 
    },
    template: '<h1>test</h1><script>console.log(foo); alert(foo);<\/script>'
  };
});

Add angular.forEach to support multiple scripts. You can do the same thing in less verbose manner if you have jQuery loaded.

Or you can just prepend that <script> with another one (since there's a mess with DOM already) where you specify the global variable, so you could skip the $window.foo = part.

Anyway, it seem to be an another 'don't try this at home' practice, there may be better ways to embed this widget.

Sign up to request clarification or add additional context in comments.

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.