【问题标题】:How to Use D3 to Angular Project from Bower Install D3如何从 Bower 使用 D3 到 Angular 项目安装 D3
【发布时间】:2016-05-21 02:07:45
【问题描述】:

我正在尝试将 D3 包含到我的 Angular 应用程序中,但似乎无法从 Bower 安装中加载 D3,因为 Angular 说它找不到它或拼写错误。该应用程序设置为使用 gulp 获取 bower_components 并从那里构建。 d3 有什么不同之处使其无法包含在 gulp 构建过程中吗?

这是我的组件,我无法访问 D3 源代码。我不应该能够从凉亭安装中访问它吗?我见过一些堆栈溢出问题,他们说要创建一个简单的“d3”模块并在工厂中从中返回一个 d3 变量,但我从 window.d3 得到 undefined ...?

   angular.module("diagram", ["d3"]).directive("ersDiagram", ["$compile", "$document", function($compile:ng.ICompileProvider, $document:ng.IDocumentService) {
    return {
        restrict: "E",
        replace: true,
        templateUrl: "diagram/template/diagram.html",
        scope: {
            nodes: "=",
            edges: "=",
            autoResizeGraph: "@?",
            enableMouseWheelZoom: "@?",
            selectedNode: "=?",
            selectedEdge: "=?",
            height: "@?",
            onSelected: "&?",
            onEdgeSelected: "&?",
            direction: "@?",
            enableZoomButtons: "@?"
        },
        controller: DiagramComponent,
        bindToController: true,
        controllerAs: "ctrl",
        link: function (scope:ng.IScope, elem:ng.IAugmentedJQuery, attrs:ng.IAttributes, ctrl:any) {
                //d3 code here
                //get undefined for all d3 code
          }

我不明白为什么我不能通过将 D3 注入应用程序来访问它?是否与 gulp 不将其包含在构建中有关?如果是常见情况,有人知道如何让 D3 包含在构建中吗?

卡了一段时间,非常感谢帮助!

如果您需要查看任何更具体的代码,请告诉我。

【问题讨论】:

    标签: javascript jquery angularjs d3.js bower


    【解决方案1】:

    在你的情况下,你没有制作 d3 模块,那么你怎么能写这样的东西angular.module("diagram", ["d3"])..

    正确的做法是首先制作一个名为 d3 的模块。 在此我们通过 ajax 加载脚本并返回它的 promise。

    angular.module('d3', [])
      .factory('d3Service', ['$document', '$q', '$rootScope',
        function($document, $q, $rootScope) {
          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 = '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 {
            d3: function() {
              return d.promise;
            }
          };
        }
      ]);
    

    在你的 html 里面做。

       <div my-directive></div>
    

    创建一个指令来查找属性my-directive

    指令的链接功能将像这样使用注入指令的d3service。

      var link = function(scope, element, attrs) {
        d3Service.d3().then(function(d3) {
          //make your svg
          var svg = d3.select(element[0])
            .append('svg')
            .style('width', '400')
            .style('height', '500')
          svg.append("circle").attr("r", 10).attr("cx", 100).attr("cy", 100).style("fill", "red");
        });
      };
    

    工作代码here

    灵感来自博客here

    【讨论】:

    • 谢谢 Cyril..我正在使用 bower install D3,所以我试图避免使用脚本标签,还是我仍然需要带有 bower 的脚本标签?如果我使用 bower install 为什么我必须创建一个 D3 模块 - 这本质上不是一个模块本身吗?我也尝试过创建一个 d3 模块,我在第一个答案的另一篇文章中看到了一些内容,但我在从工厂返回的 d3 变量中未定义:stackoverflow.com/questions/22434742/…
    • 在我的回答中,d3 模块是角度模块。它是在 angular1 中制作模块的方法。 Bower 永远不会为您服务。如果 bower 已经在您的 html 中引入了所有依赖项,那么只需将行...[d3] 放入此处angular.module("diagram", ["d3"]) 并直接在链接函数中使用 d3。
    • 好的,是的,我已经从我的所有 bower_components 中构建文件,但是当我尝试注入 d3 并直接使用它时,我得到“d3 is not defined”。我在 bower_components 中看到了 d3,但是对于一些原因我可以访问除 d3 之外的所有内容
    猜你喜欢
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多