【问题标题】:How to Dynamically Append Custom D3/SVG Element in Angular如何在 Angular 中动态添加自定义 D3/SVG 元素
【发布时间】:2015-06-21 05:44:45
【问题描述】:

每当用户单击按钮时,我都会尝试动态添加自定义元素,这将通过 D3.js 代码呈现 SVG 立方体。

我已向按钮元素添加了一个 ng-click 指令,该指令调用一个函数以动态附加自定义多维数据集元素,然后我调用 $rootScope.$apply() 以“应用”自定义指令,但是这不起作用。

完整代码如下:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>AngularJS, directives, and D3.js</title>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.js">
  </script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
 </head>

 <body ng-app="myapp" ng-controller="MyController as svgc">
  <button ng-click="svgc.addCube()">Add a Cube</button><br/>
  <cube></cube>

  <script>
    var myapp = angular.module('myapp', []);
    myapp.controller("MyController", function($rootScope) {
       var svgc = this;

       svgc.addCube = function() {
        //var cube = angular.element('cube');
          var cube = document.createElement('cube');
          document.body.appendChild(cube);
          $rootScope.$apply();
       }   
    }); 

    myapp.directive('cube', function() {
      function link(scope, el, attr) {
        // d3.select(el[0]).append('svg');
        el = el[0];

        // CUSTOM CODE FOR RENDERING A CUBE 
        var width = 300, height = 300;
        var points1 = "50,50 200,50 240,30 90,30";
        var points2 = "200,50 200,200 240,180 240,30";
        var fillColors = ["red", "yellow", "blue"];

        var svg = d3.select(el)
                    .append("svg")
                    .attr("width",  width)
                    .attr("height", height);

        // top face (parallelogram)
        var polygon = svg.append("polygon")
                         .attr("points", points1)
                         .attr("fill",   fillColors[1])
                         .attr("stroke", "blue")
                         .attr("stroke-width", 1);

         // front face (rectangle)
         svg.append("rect")
            .attr("x", 50)
            .attr("y", 50)
            .attr("width",  150)
            .attr("height", 150)
            .attr("fill", fillColors[0]);

         // right face (parallelogram)
         var polygon = svg.append("polygon")
                          .attr("points", points2)
                          .attr("fill",   fillColors[2])
                          .attr("stroke", "blue")
                          .attr("stroke-width", 1);
      }

      return {
         link: link,
         restrict: 'E'
      }
    });
  </script>
 </body>
</html>

我在 stackoverflow 上看到了使用模板添加自定义元素的示例,但这种情况需要通过 D3 动态生成 SVG 元素,所以我看不出基于模板的解决方案如何工作。

我确信解决方案很简单...欢迎提出建议:)

【问题讨论】:

    标签: angularjs svg d3.js directive


    【解决方案1】:

    我修改了代码并想出了这个解决方案:

      <script>
        var myapp = angular.module('myapp', []);
        myapp.controller("MyController", function($compile, $scope) {
           var svgc = this;
    
           svgc.addCube = function() {
              var cube = document.createElement('cube'); 
              cube.className = "cube"; 
              document.body.appendChild(cube);
              $compile(angular.element('.cube'))($scope);
           }   
        }); 
    ...
    </script>
    

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 1970-01-01
      • 2019-12-22
      • 2018-08-05
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 2011-10-05
      • 2021-12-09
      相关资源
      最近更新 更多