如何从外部js文件调用angularjs函数 - d3图表

时间:2015-12-27 19:20:47

标签: angularjs d3.js

我在Angularjs javascript文件中有以下代码,它正在为d3js雷达图提供动力

chart.js之

ui.d3.TsPlot = function(element) {
  this._element = element;
  this._plotRadius = 41;
  this._padding = 7;
  this._interpolation = 'linear-closed';
  this._editable = false;
  this._tooltips = true;
  this._animated = true;
  this._freeDraw = false;
  this._exps = [];
.........
};

ui.tsplot = function() {
       function link(scope, element, attrs) {
       var tsPlot = new ui.d3.TsPlot(element[0])
      .setPlotRadius(parseInt(attrs.plotRadius, 10))
       .setInnerRadius(parseInt(attrs.innerRadius, 10))
     // Some other assignments for TsPlot
  }
}

angular
 .module('ui.tsplot', [])
 .directive('tsPlot', ui.tsplot);

我想在模板中的javascript函数中调用tsplot()。

home.html的

  <script type="text/javascript">
   function retry()
    {
            alert('Before assignment');
            var scope = angular.element(document.getElementById("tsPlot")).scope();
            scope.ui.tsplot();
            alert('After assignment');
    }

然而,当我调用retry()函数时,第二个警报永远不会运行,当我检查控制台时,浏览器会抛出一个错误,因为“Uncaught TypeError:无法读取未定义的属性'ui'”

我在模板中使用的代码

 <script>
        var app = angular.module('tsPlotExampleApp', ['ui.tsplot']);
        app.controller('tsPlotCtrl', function ($scope) {
        $scope.dyndata={};
   });
</script>

我在retry()中缺少什么,我应该怎么做才能在重试中调用ui.tsplot()?

目的是加载d3功能,因为我动态创建了图表。

0 个答案:

没有答案