简体   繁体   English

d3之前的d3依赖项加载

[英]d3 dependency loading before d3

I am using a d3 word cloud service that requires d3 to be loaded before it in order to load properly. 我正在使用d3词云服务,该服务需要先加载d3才能正确加载。 Thus, in my services.js page, I load d3 before the word cloud service. 因此,在我的services.js页面中,我在词云服务之前加载d3。

This works most of the time. 这在大多数情况下都有效。 However, occasionally, the word cloud won't load properly and I'll get the error: 但是,有时候,单词cloud不能正确加载,并且我会收到错误消息:

Uncaught ReferenceError: d3 is not defined (localhost:8080/lib/d3-word-cloud.js:400) 未捕获的ReferenceError:未定义d3(localhost:8080 / lib / d3-word-cloud.js:400)

Is this because the services aren't loading in the proper order? 这是因为服务未按正确的顺序加载吗? What can I do to correct this? 我该怎么做才能纠正这个问题?

EDIT: Code below: 编辑:下面的代码:

myApp.factory('d3Service', ['$document', '$window', '$q', '$rootScope',
  function ($document, $window, $q, $rootScope) {
    var d = $q.defer(),
    d3service = {
      d3: function() { return d.promise; }
    };
    function onScriptLoad() {
        // Load client in the browser
        $rootScope.$apply(function() { d.resolve($window.d3); });
    }
    var scriptTag = $document[0].createElement('script');
    scriptTag.type = 'text/javascript'; 
    scriptTag.async = true;
    scriptTag.src = 'http://d3js.org/d3.v3.min.js';
    scriptTag.onreadystatechange = function () {
        if (this.readyState == 'complete') onScriptLoad();
    }
    scriptTag.onload = onScriptLoad;

    var s = $document[0].getElementsByTagName('body')[0];
    s.appendChild(scriptTag);

    return d3service;
}]);

myApp.factory('d3Cloud', ['$document', '$window', '$q', '$rootScope',
  function ($document, $window, $q, $rootScope) {
    var d = $q.defer(),
    d3cloud = {
      cloud: function() { return d.promise; }
    };
    function onScriptLoad() {
        // Load client in the browser
        $rootScope.$apply(function() { d.resolve($window.d3); });
    }
    var scriptTag = $document[0].createElement('script');
    scriptTag.type = 'text/javascript'; 
    scriptTag.async = true;
    scriptTag.src = '../lib/d3-word-cloud.js';
    scriptTag.onreadystatechange = function () {
        if (this.readyState == 'complete') onScriptLoad();
    }
    scriptTag.onload = onScriptLoad;

    var s = $document[0].getElementsByTagName('body')[0];
    s.appendChild(scriptTag);

    return d3cloud;
}]);

Not sure why it can be, but a workaround could be to check typeof d3 before using it (and wait or reload it if not). 不知道为什么会这样,但是一种解决方法是在使用它之前检查typeof d3 (如果没有,请等待或重新加载它)。

if is a web page why not load d3 on <script> and use a ready/onload method instead of doing it dynamically ? 如果是网页,为什么不在<script>上加载d3并使用ready / onload方法而不是动态地执行它?

So I guess you want to do the following at app startup: 因此,我想您想在应用启动时执行以下操作:

 angular.module('d3CloudApp', []) .run(['d3Service', 'd3Cloud', function(d3Service, d3Cloud){ d3Service.d3() .then(function(d3){ d3Cloud.cloud(); }) })] 

Should work :-) Having said that your second resolve inside d3Cloud is probably wrong: 应该工作:-)话虽如此,您在d3Cloud中的第二个解决方案可能是错误的:

 d.resolve($window.d3); 

Should be something like: 应该是这样的:

 d.resolve($window.d3Word); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM