我发现此线程How to handle multiple JS libraries with different loading times in Angular?,我正在使用它将d3.js添加到我的头文件中。我现在必须添加d3.legend.js。我将如何以回调方式完成此操作?Angular JS - 根据对方添加库
var _libraries: [
'scripts/d3.min.js',
'scripts/moment.min.js'
]
for (var i = 0, len = _libraries.length; i < len; i++) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = _libraries[i];
document.body.appendChild(s);
}
希望这有助于:
var d3js = angular.module('d3', []);
d3js.factory('d3Service', ['$document', '$q', '$rootScope', '$window',
function($document, $q, $rootScope, $window) {
var d = $q.defer();
function onScriptLoad() {
// Load client in the browser
$rootScope.$apply(function() { d.resolve($window.d3); });
}
// Create a script tag with d3 as the source
// and call our onScriptLoad callback when it
// has been loaded
var scriptTag = $document[0].createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.async = true;
scriptTag.src = 'lib/d3.v3.js';
scriptTag.onreadystatechange = function() {
if (this.readyState == 'complete') onScriptLoad();
}
scriptTag.onload = onScriptLoad;
var s = $document[0].getElementsByTagName('body')[0];
s.appendChild(scriptTag);
//this is now where I have to add d3.legend.js which depends on the successful loading of d3.js.
return {
d3: function() { return d.promise; }
};
}]);