Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: How to make angular load script inside ng-include?

Tags:

angularjs

Hey I am building a web page with angular. The problem is that there are somethings already build without angular and I have to include them as well

The problem is this.

I have something like this in my main.html:

<ngInclude src="partial.html"> </ngInclude> 

And my partial.html has something like this

<h2> heading 1 <h2> <script type="text/javascript" src="static/js/partial.js"> </script> 

And my partial.js has nothing to do with angularjs. nginclude works and I can see the html, but I can not see the javascript file being loaded at all. I know how to use firebug/ chrome-dev-tool, but I can not even see the network request being made. What am I doing wrong?

I knwo angular has some special meaning to script tag. Can I override it?

like image 670
Ranjith Ramachandra Avatar asked Aug 30 '12 13:08

Ranjith Ramachandra


People also ask

What is Ng-include in AngularJS?

AngularJS ng-include Directive The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename.

Does Ng-include create a new scope?

In this case, where you need to load templates from other domains or other protocols, then you can add them to your trusted resource URL list, which will set the url as a trusted url in your application. The ng-include directive is executed at priority level -400 and creates new scope every time it is invoked.

What is include in angular?

AngularJS has a built-in directive to include the functionality from other AngularJS files by using the ng-include directive. The primary purpose of the ng-include directive is used to fetch, compile, and include an external HTML file in the main AngularJS application.


2 Answers

The accepted answer won't work from 1.2.0-rc1+ (Github issue).

Here's a quick fix created by endorama:

/*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)); 

Simply add this file, load ngLoadScript module as application dependency and use type="text/javascript-lazy" as type for script you which to load lazily in partials:

<script type="text/javascript-lazy">   console.log("It works!"); </script> 
like image 115
Paolo Moretti Avatar answered Sep 24 '22 11:09

Paolo Moretti


Short answer: AngularJS ("jqlite") doesn't support this. Include jQuery on your page (before including Angular), and it should work. See https://groups.google.com/d/topic/angular/H4haaMePJU0/discussion

like image 43
Mark Rajcok Avatar answered Sep 22 '22 11:09

Mark Rajcok