I have improved endorama's solution at github
The same process.
- Create the angular-loadscript.js (from the link above)
in your app use 'ngLoadScript' as a resource dependency.
var app = angular.module('YOUR_APP_NAME', ['ngResource','ngRoute', ...,'ngLoadScript']);
In your partial use 'text/javascript-lazy' as the MIME type.
Everything should work as required:
/*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();
/*var f = new Function(code);
f();*/
}
}
};
});
}(angular));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…