如何在生成的d3 html中使用angularjs指令? [英] How do I use angularjs directives in generated d3 html?
问题描述
我想在我的d3可视化中使用angularjs tooltip指令 ,所以我有一些像
I'm trying to use the angularjs tooltip directive on my d3 visualisation, so I have something like
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("circle")
.attr("tooltip-append-to-body", true)
.attr("tooltip", function(d) {
return d.name;
})
// ... attributes
但是,工具提示未显示。我需要 $ compile
还是什么?我已经尝试包装它 $ timeout
,但是没有工作。
However, the tooltips are not showing. Do I need to $compile
or something? I've tried wrapping it around $timeout
too, but that didn't work.
推荐答案
我有一个类似的问题,是的,解决它与 $ compile
。我假设你的d3代码是在一个自定义指令。从那里你可以添加你的工具提示属性,删除你的自定义指令属性,所以$ compile只运行一次,并调用$ compile:
I had a similar problem and yes, solved it with $compile
. I'm assuming your d3 code is inside a custom directive. From there you can add your tooltip attributes, remove your custom directive attribute so $compile only runs once, and call $compile:
myApp.directive('myNodes', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var nodes = [{"name": "foo"}, {"name": "bar"}]
var mySvg = d3.select(element[0])
.append("svg")
.attr("width", 100)
.attr("height", 100);
var node = mySvg.selectAll(".node")
.data(nodes)
.enter()
.append("circle")
.attr("cx", function(d,i){
return 20+i*50;
})
.attr("cy", 50)
.attr("r", 10)
.attr("tooltip-append-to-body", true)
.attr("tooltip", function(d){
return d.name;
});
element.removeAttr("my-nodes");
$compile(element)(scope);
}
};
}]);
$ compile服务确保您的元素使用指令添加的属性进行编译。
The $compile service makes sure your element is compiled with the attributes added by your directive.
这里是一个工作小提琴使用上述代码。希望这是你要找的!
Here is a working fiddle using the above code. Hope it's what you're looking for!
这篇关于如何在生成的d3 html中使用angularjs指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!