angularjs - 如何在生成的 d3 html 中使用 angularjs 指令?

标签 angularjs d3.js angularjs-directive angular-ui

我正在尝试使用 angularjs tooltip directive在我的 d3 可视化中,所以我有类似的东西

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 周围,但这没有用。

最佳答案

我有一个类似的问题,是的,用$compile解决了它。我假设您的 d3 代码位于自定义指令内。从那里您可以添加工具提示属性,删除自定义指令属性,以便 $compile 仅运行一次,然后调用 $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 服务确保您的元素是使用指令添加的属性进行编译的。

Here is a working fiddle使用上面的代码。希望这就是您正在寻找的!

关于angularjs - 如何在生成的 d3 html 中使用 angularjs 指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20399336/

相关文章:

c# - ASP.NET 与在 C# 中创建/打开 .HTML 文件

javascript - 为什么我的 Angular 代码似乎正在回发

javascript - 类型错误 : undefined is not a function --- Need to troubleshoot

javascript - 使用 D3 创建 HTML 表格时,将某些数据用于行类而不是单元格

javascript - scaleBand 轴中的数据点和刻度未对齐

javascript - 为 Angular Directive(指令)添加幻灯片动画

javascript - 具有双向绑定(bind)的隔离范围指令不反射(reflect) Controller 范围的变化

javascript - "Static"AngularJS 中的属性

javascript - 如何判断单击时是否按住了键

javascript - Angular $broadcast 仅在页面刷新时填充数据