[英]Angular Directive inside d3 node
I've got a simple version of the tree layout ( https://bl.ocks.org/mbostock/4339083 ) on my Angular app. 我在Angular应用中有一个简单的树形布局版本( https://bl.ocks.org/mbostock/4339083 )。 However, I'm trying to replace the "text" node with an Angular Directive. 但是,我试图用Angular Directive替换“ text”节点。 How can I achieve this? 我该如何实现?
Instead of .text(function(d) { return d.name; })
, I'm trying to compile a directive which takes in d.name as a parameter. 我正在尝试编译以d.name为参数的指令,而不是.text(function(d) { return d.name; })
。
VERSIONS : 版本 :
Update: The compile works great, it injects the HTML into it, however it doesn't show up in the SVG. 更新:编译效果很好,它向其中注入了HTML,但是未在SVG中显示。 When you hover over the user-node
node in the DOM, it doesn't highlight anything on the page... 当您将鼠标悬停在DOM中的user-node
节点上时,它不会在页面上突出显示任何内容...
Your questions a little vague but perhaps use a $compile ? 您的问题有点含糊,但也许使用$ compile ?
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeEnter.each(function(d){
var el = $compile( '<node-directive></node-directive>' )( scope );
angular.element(this).append(el);
});
EDITS 编辑
I worked up an example. 我举了一个例子。 It seems that SVG doesn't like the nesting of custom elements (your directives inside of it). 似乎SVG不喜欢自定义元素的嵌套(您内部的指令)。 So, here's an approach: 因此,这是一种方法:
nodeEnter.append("node-directive")
.attr("data-name", function(d){
return d.name;
})
.each(function(){
$compile(this)(scope);
});
Where node-directive
is: 其中node-directive
是:
angular.module('d3Chart').directive('nodeDirective', [
function() {
return {
link: function(scope, element, attr) {
var p = d3.select(element[0].parentNode);
p.append("text")
.text(attr.name);
element.remove();
}
};
}
]);
Complete code: 完整的代码:
var myAppModule = angular.module('d3Chart', []); angular.module('d3Chart').controller('chartCtrl', function($scope) { $scope.treeData = [{ "name": "TopLevel", "parent": "null", "value": 10, "type": "black", "level": "red", "children": [{ "name": "Level 2A", "parent": "Top Level", "value": 15, "type": "grey", "level": "red", "children": [{ "name": "Son of A", "parent": "Level 2 A", "value": 5, "type": "steelblue", "level": "orange" }, { "name": "Daughter of A", "parent": "Level 2 A", "value": 8, "type": "steelblue", "level": "red" }] }, { "name": "Level 2B", "parent": "Top Level", "value": 10, "type": "grey", "level": "green" }] }]; }); angular.module('d3Chart').directive('nodeDirective', [ function() { return { link: function(scope, element, attr) { var p = d3.select(element[0].parentNode); p.append("text") .text(attr.name); element.remove(); } }; } ]); angular.module('d3Chart').directive('treeLayout', ['$compile', function($compile) { return { restrict: 'E', scope: { data: '=' }, link: function(scope, element) { var margin = { top: 20, right: 120, bottom: 20, left: 120 }, width = 960 - margin.right - margin.left, height = 500 - margin.top - margin.bottom; var i = 0; var tree = d3.layout.tree() .size([height, width]); var diagonal = d3.svg.diagonal() .projection(function(d) { return [dy, dx]; }); var svg = d3.select(element[0]).append("svg") .attr("width", width + margin.right + margin.left) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); root = scope.data[0]; update(root); function update(source) { // Compute the new tree layout. var nodes = tree.nodes(root).reverse(), links = tree.links(nodes); // Normalize for fixed-depth. nodes.forEach(function(d) { dy = d.depth * 180; }); // Declare the nodes… var node = svg.selectAll("g.node") .data(nodes, function(d) { return d.id || (d.id = ++i); }); // Enter the nodes. var nodeEnter = node.enter().append("g") .attr("class", "node") .attr("transform", function(d) { return "translate(" + dy + "," + dx + ")"; }); nodeEnter.append("circle") .attr("r", function(d) { return d.value; }) .style("stroke", function(d) { return d.type; }) .style("fill", function(d) { return d.level; }); nodeEnter.append("node-directive") .attr("data-name", function(d){ return d.name; }) .each(function(){ $compile(this)(scope); }); // Declare the links… var link = svg.selectAll("path.link") .data(links, function(d) { return d.target.id; }); // Enter the links. link.enter().insert("path", "g") .attr("class", "link") .style("stroke", function(d) { return d.target.level; }) .attr("d", diagonal); } } } } ])
<!DOCTYPE html> <html ng-app="d3Chart"> <head> <script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script> <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script> <script data-require="jquery@2.1.4" data-semver="2.1.4" src="https://code.jquery.com/jquery-2.1.4.js"></script> <script src="script.js"></script> </head> <body> <div ng-controller="chartCtrl"> <tree-layout data="treeData"></tree-layout> </div> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.