刷新范围变量不适用于范围功能 [英] Refreshing scope variable not working with scope function

查看:174
本文介绍了刷新范围变量不适用于范围功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用chartJS及其包装角度图更新我的图表数据。单击一个按钮时,它会在图表上添加数据,没问题。

I'm trying to update data of my chart using chartJS and its wrapper angular-chart. When a button is clicked, it adds data on the chart, no problem with this.

我有另一个事件,它在滚动事件后更新图表。该呼叫效果很好,它调用与以前完全相同的功能。但是我的$ scope.valuesData没有更新。然后我在函数之后添加了一个 $ scope。$ apply(),并用正确的值重绘整个图表。

I have another event, which update the chart after a scroll event. The call works great, and it calls exactly the same function as previous. But my $scope.valuesData isn't updated . Then I added a $scope.$apply() after the function, and it redraws the whole chart with the right values.

问题是:


  1. 为什么我的$ scope.chartValues当我从控制器调用AddData函数时没有更新(为什么当我从DOM调用函数时$ scope.chartValues被更新)。这可能是一个数据绑定问题?

  1. Why does my $scope.chartValues is not updated when I call the function AddData from the controller (and why is $scope.chartValues is updated when I call the function from the DOM). It is probably a data-binding issue ?

这是一个只更新特定范围变量而不是刷新整个范围的函数吗?

Is it a function that update only a specific scope variable, instead of refreshing the whole scope ?

我的代码:

HTML:

<ion-scroll zooming="false" direction="x" delegate-handle="scroller" on-scroll="getScrollPosition()" has-bouncing="true" scroll-event-interval="30000">
  <div class="chart_wrapper" ng-style="{ 'width': myWidth + '%' }"> 
      <canvas  class="chart-bar" chart-data="data2" chart-labels="labels" chart-dataset-override="datasetOverride2" chart-options="options2" ng-click="goToMetrics()" ">
      </canvas>    
 </div>
</ion-scroll>

<button class="button button-small button-calm" ng-click="addValues()">Add Data </button>

JS(控制器):

  $scope.getScrollPosition = function(){
  var pos =             $ionicScrollDelegate.$getByHandle('scroller').getScrollPosition().left ; 
  if (pos == 0 && $scope.flag == -1){
     $scope.flag = 0;
     //setTimeout($scope.addValues(), 1000);
     $scope.addValues();
  }
  if (pos < 0){
    $scope.flag = -1;
  }
 };


  $scope.addValues = function(){
  console.log('add');
  // Retrieve values
  var week = Graph.getData();
  var pp = $scope.chartData; 
  var mm = $scope.labels;

  // Add values to the chart and update the chart width 
  var tmp = Graph.addBegin(pp, mm,  week);
  $scope.valuesData = tmp.values; 
  $scope.myWidth = $scope.myWidth + tmp.length * 4 ; 
 };

感谢任何帮助

推荐答案


  1. 因为setTimeout在角度上下文之外运行,所以使用$ timeout version。

  2. 不行。您可以使用$ scope。$ digest()来检查当前范围的更改,而不是所有$ scope树;

这篇关于刷新范围变量不适用于范围功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆