0

I have got a simple example to try to figure out why my angular partial template can not load another javascript component.

I am using 1 template (singleArticleView.html) and a parent page(blog.html)and i am trying to apply run_prettify.js to a simple code snippet inside of my template. If the code is inside of the template it does not work. If i remove the code and the script tags from the child template and include them inside of the parent it works properly

How can I execute that javascript library inside my template? I think it does not work because DOM elements have loaded later.

How can I evaluate if the content in the partial template is already rendered in order to execute any external javascript code to manipulate the DOM?

I appreciate any suggestion

It is the code:

blog.html (Parent)

   <script src="~/AngularJS/Controllers/articlesController.js"></script>
    <script src="~/AngularJS/Controllers/singleArticleController.js"></script>
            <div class="row">
                    <div class="col-md-8 col-md-offset-1">
                        <div data-ng-view>
                        </div>
                    </div>
            </div>

singleArticleView.html (template)

            <script src="~/Scripts/run_prettify.js"></script>
            <link href="~/Content/prettify.css" rel="stylesheet" />

                         <div>
                                {{article.author }}
                                <pre class="prettyprint linenums lang-html">
                                            &lt;!DOCTYPE html&gt;
                                            &lt;html&gt;
                                              &lt;head&gt;
                                                &lt;title&gt;Demo | Prettify.JS&lt;/title&gt;
                                              &lt;/head&gt;
                                              &lt;body&gt;
                                                &lt;h1&gt;Hello, World!&lt;/h1&gt;
                                              &lt;/body&gt;
                                            &lt;/html&gt;
                                </pre>

                            </div>

singleArticleControlle.js (controller)

(function () {
                var app = angular.module("dbayonaCode");
                var singleArticleController = function ($scope, dataService, $window, $routeParams, $sce) {

                    $scope.article = {};
                    $scope.newComment = {};
                    //Get the article with its comments to show in the template and add a new comment
                    //It uses ArticlesController.cs
                    dataService.getArticleById($routeParams.id)
                     .then(function (data) {
                         //success
                         $scope.article = data;
                         $scope.myContent = $sce.trustAsHtml(data.body);

                     },

                     function () {
                         //error
                         $windows.location = "#/";

                     });

                }
                app.controller("singleArticleController", singleArticleController);
            }());

1 Answer 1

1

This is normal behavior for angular, which strips script tags from templates.

I found a couple solutions, depending on exactly what you are trying to do. If it's a smaller script and you can just inline it, I like this solution here which adds a 'lazy-loading' type to the script tag:

/*global angular */
(function (ng) {
  'use strict';

  var app = ng.module('ngLoadScript', []);

  app.directive('script', function() {
    return {
      restrict: 'E',
      scope: false,
      link: function(scope, elem, attr) {
        if (attr.type === 'text/javascript-lazy') {
          var code = elem.text();
          var f = new Function(code);
          f();
        }
      }
    };
  });

}(angular));

The above code comes from this post: AngularJS: How to make angular load script inside ng-include?

The problem is that it only loads the text between the script tags and won't load a file specified in the src attribute.

A different solution also modifies the script directive and allows us to load external js files via the src attribute, or from between the script tag if src is undefined:

/*global angular */
(function (ng) {
  'use strict';

  var app = ng.module('ngLoadScript', []);

  app.directive('script', function() {
    return {
      restrict: 'E',
      scope: false,
      link: function(scope, elem, attr) 
      {
        if (attr.type==='text/javascript-lazy') 
        {
          var s = document.createElement("script");
          s.type = "text/javascript";                
          var src = elem.attr('src');
          if(src!==undefined)
          {
              s.src = src;
          }
          else
          {
              var code = elem.text();
              s.text = code;
          }
          document.head.appendChild(s);
          elem.remove();
        }
      }
    };
  });

}(angular));

The above code snippet comes from here.

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

6 Comments

i am not using ngInclude, i am using ng-view. However I have created the js file with that content and calling that file after I call the controller assigned to that template. I guess the name "ngLoadScript" on that line " var app = ng.module('ngLoadScript', []);" is the name of my module. I dont know what exactly they guys in the other links mean with "load ngLoadScript module as application dependency".? , anyway, i can see that the client is getting the js files using the 'lazy-loading' tag, now i am getting a console error:"Uncaught Error: can't load XRegExp twice in the same frame" :(
Look at the plunker I have it working with ng-transcode and ng-view.
Could you please send me the link to the plunker example. Thanks
Sorry I see now I neglected to include it. I'll post it shortly
Could u please include it as a comment. I would really appreciate it.
|

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.