【问题标题】:Angular directive works only the first timeAngular 指令只在第一次工作
【发布时间】:2014-05-08 10:07:29
【问题描述】:

我创建了 amCharts 指令,它以某种方式仅在第一次起作用,第二次在我清除浏览器缓存之前不会显示。 另一个问题是它只能在 Chrome 中工作:/

这是我的代码:

.directive('amGraph', function(){
    return {
        restrict: 'E',
        replace: true,
            scope: {
            getArchiveData: '='
        },
        template: '<div id="chartdiv"></div>',
        link: function (scope, element, attrs){

            scope.$watch('getArchiveData', function(data){

                 var chart = false;

                 var initChart = function(){
                     if (chart) chart.destroy();

                     chart = new AmCharts.makeChart("chartdiv", {
                          ...
                     });
                 };

                 initChart();

            });
        }
     }
 })

这是 HTML:

<div ng-controller="dataCtrl">
<am-graph get-archive-data="getData"></am-graph>
</div>

【问题讨论】:

  • 你能在 Fiddle 中展示这个问题吗?为什么不使用隔离范围?
  • 从您在这里介绍的情况来看,这似乎不是角度问题。
  • @MaximShoustin 为什么要使用隔离范围?
  • @arturgrzesiak 那是什么? :D
  • 我现在正在尝试使用隔离范围。

标签: javascript angularjs angularjs-directive angularjs-scope


【解决方案1】:

它在这里工作:http://jsfiddle.net/tkX89/ 可能是你的 HTML 写错了。角度没有问题。

虽然提示:

replace: true 

将用分配的模板替换您的指令标记。小心点。

PS:使用工厂获取数据非常棒。继续前进!!

【讨论】:

  • 我认为这不是 HTML 问题。指令第一次工作得很好,在我重新加载页面后没有出现任何错误。感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
相关资源
最近更新 更多