简体   繁体   English

使用Angular指令构建动态矩形

[英]Build dynamic rectangle with Angular directive

Recently I have written a project with D3, so I need a dynamic rectangle. 最近,我用D3编写了一个项目,因此我需要一个动态矩形。 I used Angular to create a dynamic visualization.I have two input type rang , the first one will change the 'width' of rectangle and the second will change the 'height'.However I don't know how to use angular to draw a dynamic rectangle. 我用角创建动态visualization.I具有两个input类型rang ,第一个将改变矩形的“宽度”和第二将改变“height'.However我不知道如何使用角度来绘制动态矩形。 This is my code: 这是我的代码:

<div ng-app="myApp"> 
   <rect-designer/>
   <div>
     <input type="range"  ng-model="rectWidth" min="0" max="400" value="0"/>
     <input type="range" ng-model="rectHeight" min="0" max="700" value="0"/>
   </div>
</div>

Here is my JavaScript code: 这是我的JavaScript代码:

var App = angular.module('myApp', []);
App.directive('rectDesigner', function() {
    function link(scope, el, attr) {

 var svgwidth=1000, svgheight=600;
 var svgContainer = d3.select(el[0]).append('svg').attr('id','svgcontainer')
      .attr({ width: svgwidth, height: svgheight });

 scope.$watchGroup(['rectWidth','rectHeight'], function () {

      svgContainer.append("rect").attr("id", "Rect")
       .attr({ width: rectWidth, height: rectHeigh })
       .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')')


   },true);
  }return {

   link: link,
   scope: {
       rectHeigh: '=',
       rectWidth: '=',

   },
   restrict: 'E'
 };
}); 

I don't know if there is any way to make svgWidth and svgheight dynamic, I used this code but the result was undefined . 我不知道是否有任何方法可以使svgWidthsvgheight动态化,我使用了这段代码,但是结果是undefined

   scope.$watch(function(){
           svgWidth = el.clientWidth;
           svgHeight = el.clientHeight;
    });

You are missing some basics here: 您在这里缺少一些基础知识:

  1. You don't have a controller. 您没有控制器。
  2. The variables you are watching are not part of your directive but they should be part of that missing controller. 您正在监视的变量不是您的指令的一部分,但应该是缺少的控制器的一部分。
  3. Since these variables aren't part of the directive, there's no need to return them into it's scope (again, they will be in the controller). 由于这些变量不是指令的一部分,因此无需将它们返回到它的作用域中(同样,它们将在控制器中)。
  4. $scope.watchGroup has a callback with a function of newValues . $scope.watchGroup有一个具有newValues函数的回调。 This is where the changed variables will be. 这是更改后的变量所在的位置。
  5. You want to append the rect to the svg and then manipulate it's width/height. 您想要将rect附加到svg,然后操纵其宽度/高度。 You don't want to re-append it each time the width/height changes. 您不想每次宽度/高度更改时都重新添加它。

So putting all this together: 因此,将所有这些放在一起:

var App = angular.module('myApp', []);

var Ctrl = App.controller('myCtrl', ['$scope', function($scope) {

  // I always like to give them defaults in code
  $scope.rectWidth = 50;
  $scope.rectHeight = 50;

}]);

Ctrl.directive('rectDesigner', function() {

  function link(scope, el, attr) {

    var svgwidth = 500,
      svgheight = 600;

    var svgContainer = d3.select(el[0])
      .append('svg')
      .attr('id', 'svgcontainer')
      .attr({
        width: svgwidth,
        height: svgheight
      });
    // only append one rect
    var rect = svgContainer
      .append("rect")
      .attr("id", "Rect")
      .attr('transform', 'translate(' + svgwidth / 2 + ',' + svgheight / 2 + ')');

    scope.$watchGroup(['rectWidth', 'rectHeight'], function(newValues) {

      var width = newValues[0];
      var height = newValues[1];

      // now change it's width and height
      rect.attr({
        width: width,
        height: height
      });

    }, true);
  }
  return {
    link: link,
  };
});

Example here . 这里的例子。

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

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