如何将angularjs指令绑定到一个链接函数定义的d3js节点 [英] How to bind an angularjs directive to a d3js node defined in a link function
问题描述
我想在一个AngularJs应用程序使用d3js图形,然后绑定指令到节点。
I would like to use a d3js graph in an AngularJs app, and then bind directives to the nodes.
首先,我把JS code在指令的纽带作用,一切运行良好。
First I put the js code in the link function of a directive and everything works well.
angular.module('myApp', []).
directive('grapheForces', function() {
return {
restrict: 'A',
link: function (scope, element) {
var width = 450;
var height = 400;
var color = d3.scale.category20();
scope.$watch('grapheDatas', function (grapheDatas) {
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height])
.nodes(grapheDatas.nodes)
.links(grapheDatas.links)
.start();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var link = svg.selectAll(".link")
.data(grapheDatas.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(grapheDatas.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
}
}
}).
然后我想一个提示添加到节点上,所以我去:
And then I would like to add a tooltip to the nodes, so I go :
var node = svg.selectAll(".node")
.attr("tooltip", function(){
return "tooltipTextHere";
});
由于我用角引导,提示是一种指令。
工具提示属性是HTML结果以及present:
As I use angular-bootstrap, tooltip is a directive. The tooltip attribute is well present in the html result :
<circle tooltip="tooltipTextHere" class="nodeCircle" r="4.5" style="fill: #b0c4de;"></circle>
但提示是无效的,因此它也适用于每一个绑定我这样的指令。
But the tooltip is ineffective, and so it goes for every directive I bind like that.
我想这是因为该指令尚未在编译阶段采取的帐户,但我无法找到如何做到这一点,因为我到达AngularJs我目前的COM prehension限制。
I guess it is because the directive has not been taken in account during the compile phase, but I can't find how to do that as I reach my current comprehension limits in AngularJs.
你有我怎么能做到这一点的想法?
非常感谢你为你的非常有价值的答案。
Do you have an idea of how i could make it happen ?? Thank you very much for your very valuable answers.
推荐答案
我已经回答了一个非常类似的<一个href=\"http://stackoverflow.com/questions/20399336/how-do-i-use-angularjs-directives-in-generated-d3-html/\">question这里。你可以看到完整的答案在那里。
I have answered a very similar question here. You can see the full answer there.
基本上,你添加提示属性后,您需要删除的东西您的自定义指令属性如 element.removeAttr(graphe部队)
然后运行 $编译(元)(范围)
这样的应用程序会发现提示指令。
Basically, after you add the tooltip attributes, you need to remove your custom directive attribute with something like element.removeAttr("graphe-forces")
and then run $compile(element)(scope)
so the app will find the tooltip directive.
下面是一个工作示例的小提琴。
Here's a fiddle of a working example.
这篇关于如何将angularjs指令绑定到一个链接函数定义的d3js节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!