如何在生成的 d3 html 中使用 angularjs 指令? [英] How do I use angularjs directives in generated d3 html?
问题描述
我正在尝试在我的 d3 可视化中使用 angularjs 工具提示指令,所以我有类似的东西
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.
这是一个使用上述代码的工作小提琴.希望这是你要找的!
这篇关于如何在生成的 d3 html 中使用 angularjs 指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!