【问题标题】:Array sets to null when bound to highcharts/highmaps directive绑定到 highcharts/highmaps 指令时,数组设置为 null
【发布时间】:2017-05-21 16:27:12
【问题描述】:

我正在使用 highmaps 创建一个矢量地图,该矢量地图可以根据加载到 series[0].data 数组中的数据值进行更改。初始图表是使用我的默认值创建的,并使用自定义指令插入到页面中。 When a select dropdown changes value, I fire off a function using ng-change to update the chart values.

我的ng-change 函数按预期触发并运行,直到我尝试访问包含用于填充高图的数据的系列。似乎在创建初始图表后,序列数组被赋予了null 的值。即使我尝试加载新的 chartOptions 对象,这种情况仍然存在。我注意到,当包含 chartOptions 的 $scope 对象未绑定到指令时,该系列将填充正确的值。该数组仅在绑定到指令时为空。

这只发生在我的地图上。页面上的所有其他 highcharts 在被双向绑定到指令后,它们的系列数组都可以编辑。我的 highmap chartOptions 对象的所有其他元素也可以查看和编辑,而不是包含实际数据的部分。

我的指令

.directive('mapChart', function(){
return {
        restrict: "E",
        replace: true,
        template: "<div id='map' class='div_content-holder'></div>",
        scope:{
            options:"=",
        },
        link: function(scope, element){
                Highcharts.mapChart(element[0], scope.options);
            });
        }
    };
});

我的html

<map-chart options="region_options"></map-chart>

我的 JavaScript

$scope.region_options = {};

var setRegionMap = function(jsonData){
        var chartOptions = selectChart('map'); // grabs my premade options object 

        // grabs data from somewhere else and adds it to chart options
        chartOptions.series[0].data.forEach(function (region, index) {
           region["value"] = $scope.region_map.one_month[index];
           console.log(chartOptions.series[0].data); //says that series exists
        });
        $scope.region_options = chartOptions;
        console.log($scope.region_options); //says that .series is null, everything else is still there
        //$scope.$apply(); //i do this elsewhere  in my ajax call
    };

这是使用 highmaps 或 angular 或两者都绑定数组的双向绑定的问题吗?

【问题讨论】:

  • 是 map-chart-javascript 打算成为 map-chart 吗?在你的元素指令中我的意思是
  • @quirimmo 啊,是的,正在编辑它以供发布。固定。
  • 什么是$scope.region_options?是$scope.options吗?
  • @quirimmo 是的,我将$scope.region_options 加载到$scope.options 中,将region_options 从控制器传递到指令。
  • 所以只是为了更好地了解发生了什么。如果你打印 console.log(chartOptions);你有你的系列。如果你打印 console.log($scope.region_options);分配后,您的系列没有了,但所有其他数据都在那里?

标签: javascript angularjs arrays highcharts highmaps


【解决方案1】:

您可以通过以下方式更改指令,为当前数据添加新参数。 添加两个手表,您将确保每次从外部更改数据时,都会刷新图表以设置正确的数据,并且在选项上的第一个手表上,您将确保在您的选项真正加载时实例化图表:

.directive('mapChart', function(){
  return {
    restrict: "E",
    replace: true,        
    template: "<div id='map' class='div_content-holder'></div>",   
    scope:{
      options:"=",
      current: "=" // your actual data        
    },        
    link: function(scope, element){
      var chartexample = {};       
      scope.$watch('options', function() {
        chartexample = Highcharts.mapChart(element[0], scope.options);
      });
      scope.$watch('current', function(){
        chartexample.series[0].setData(scope.current);
      });            
    }
  }
});

你可以摆脱这两个观察者。 关于初始化,您只需要确保在您已经拥有所有选项后初始化您的指令,因此您不再需要选项上的手表。 对于当前数据,可以在指令中添加回调参数&amp;,用于更新图表的值,并在与控制器内部select的ngChange关联的函数中调用该指令的回调。

这将使代码更好更简洁。 但是现在你可以通过这种方式享受你的图表:)

祝你的应用好运!

干杯:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多