【问题标题】:Passing ngInit $index back to script将 ngInit $index 传回脚本
【发布时间】:2014-03-16 20:36:53
【问题描述】:

我目前正在开发一个包含 AngularJS 和 D3.js 的可视化工具。 我需要将 ng-init 的索引传回脚本,以唯一设置多个图表的 id 和类。

这是我的代码示例。

<body ng-app = "chartApp" ng-init="charts = [[4,2,3,4,42,32], [123,52,14,22,12], [53,72,76,13,13], [143,652,76,112]]">
<donut-chart data="chart" ng-repeat="chart in charts"> {{ $index + 1 }} </donut-chart>

我想把 $index 带回脚本,这样我就可以唯一地设置 id 或 class attr

var chartApp = angular.module('chartApp', [])

chartApp.directive('donutChart', function(){
function link(scope, el){
    var data = scope.data
    var color = d3.scale.category20()
    var el = el[0]
    var width = el.clientWidth
    var height = el.clientHeight
    var min = Math.min(width, height)
    var pie = d3.layout.pie().sort(null)
    var arc = d3.svg.arc()
                .outerRadius(min / 2 * 0.9)
                .innerRadius(min / 2 * 0.5)
    var svg = d3.select(el).append('svg')
                .attr({width: width, height: height})
                .append('g')
                    .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')')

    svg.selectAll('path')
        .data(pie(data))
        .enter().append('path')
            .attr("id", function(d, i) { return "arc" + (i+1); })
            .style('stroke', 'white')
            .attr('d', arc)
            .attr('fill', function(d, i){ return color(i) })

}

return {
    link: link,
    restrict: 'E',
    scope: { data: '=' }
}
})

我已经将圆环图的弧线设置为唯一,但我无法将圆环图本身设置为唯一。

有没有办法做到这一点?

【问题讨论】:

    标签: javascript angularjs d3.js visualization


    【解决方案1】:

    哇,不敢相信答案很简单, 我只需添加一个新范围即可将 $index 传递回脚本。

    发布几分钟后得到答案。 为任何可能会觉得这很有用的人更新这篇文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多