Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角形$watch不';不要被触发_Javascript_Angularjs - Fatal编程技术网

Javascript 角形$watch不';不要被触发

Javascript 角形$watch不';不要被触发,javascript,angularjs,Javascript,Angularjs,我正在尝试编写一个用于绘制条形图的指令,我希望它在每次数据更改时重新绘制图表。我目前有: /* This directive will create a bar chart based on a dataTable. * It expects the first column of the data table to contain the labels. * If more than 2 columns (labels plus more than one value) are supp

我正在尝试编写一个用于绘制条形图的指令,我希望它在每次数据更改时重新绘制图表。我目前有:

/* This directive will create a bar chart based on a dataTable. 
 * It expects the first column of the data table to contain the labels.
 * If more than 2 columns (labels plus more than one value) are supplied,
 * all bars generated by values in one row will be grouped together, making 
 * an easy visual comparison (benchmark).
 * If the option overlay is supplied, the odd value columns will be drawn
 * underneath the even values (Ex [label, col1, col2, col3, col4], with overlay = true will result 
 * in col1 and col3 to be drawn underneath col2 and col4
 */


angular.module('kapstok').directive('nBarChart', ['$window', '$location', '$parse', '$timeout', 
    function($window, $location, $parse, $timeout){
  return{
    restrict: 'E',
    scope: true,
    link: function(scope, elm, attrs){

      // Read all the different parameters givin in the directive declaration.
      // If options are not given, they are replaced by their default values.
      var dataGetter        = $parse(attrs.data);
      var data = dataGetter(scope) || undefined;
      if(!data){
        throw Error("No (valid) data source specified!");
      }



          // Redraw the table once the table gets updated. However, usually multiple elements
      // of the table will be updated at the same time, while the watch will get triggered 
      // at each update. To prevent this, we give it a timeout of half a second,
      // allowing all the updates to be applied before the table is redrawn.
      scope.$watch(function(){ return data.getVersion(); }, $timeout(drawChart, 500));
然后,在控制器中,我执行以下操作:

controlmodule.controller('PrefscanController',['$http', '$scope', 'DataTable', function($http, $scope, DataTable){
  $scope.myData = new DataTable();
  $scope.myData.addColumn("string", "label");
  $scope.myData.addColumn("number", "survey");
  $scope.myData.addColumn("number", "benchmark");
  $scope.myData.addColumn("number", "break-it");

  $scope.myData.addRow(['test', 20, 10, 4]);
  $scope.myData.addRow(['test2', 42, 13, 2]);


  $scope.hideBenchmark = function(){
    console.log("myData.version = ", $scope.myData.getVersion());
    $scope.myData.hideColumn(2);
    console.log("myData.version = ", $scope.myData.getVersion());
  }
在dataTable中的所有函数中,每当更改任何内容时,一个名为“version”的属性都会递增,这就是我希望$watch看到的

在我的模板中,我有一个带有ng单击的按钮,它调用hideBenchmark函数。此函数确实会被调用,dataTable版本会被更新,但无论我尝试什么,手表都不会被触发

我尝试在我的hideBenchmark函数中调用$scope.$appy()/$scope.$digest(),尝试向手表添加“true”(查找对象不平等),尝试观看“attrs.data”、“data.version”,但似乎都没有触发手表!我知道我可以在作用域上创建另一个独立于此数据的变量,观察它,并在每次更改数据时更改它,但这似乎是丑陋和不必要的

有人知道为什么手表不会被触发吗?我怎样才能让它做我想做的事

问候,


Linus

根据Sunil的建议,您应将其纳入范围:

scope: {
    data: '='
} 
因为如果您不这样做,数据不会在指令中更新

这里是小提琴:

您将函数调用传递给$watch,我认为它应该是引用或声明

  scope.$watch(function(){ 
      return data.getVersion(); 
  }, function(){ 
      $timeout(drawChart, 500)
  });
显然,
drawChart
应该在该上下文中定义,这样才能起作用。。。您可以尝试查看$watcher是否被触发,并记录版本的值

  scope.$watch(function(){ 
      return data.getVersion(); 
  }, function(v){ 
      console.log(v)
  });

注意:对于所有其他表示需要将其添加到范围的响应,这是错误的。您不需要这样做,因为您正在将自己的自定义函数传递给$watcher。使用
$parse
服务,您获取“数据”元素的方式很好。

它当前不起作用的原因是外部作用域(控制器)通知内部作用域(指令)它应该消化——这是因为某些内容已被更改。 这是因为数据对象不属于指令范围。 处理此问题的两种方法:
首先,您可以将数据对象传递给指令并创建一个独立的作用域:

scope: {data: '='}
或者,在您的案例中,您可以传递一个函数指针,以便在数据更改时收到通知(hideBenchmark):

scope: {hideBenchmark: '@'}
是第一种方法的plunker。

您是否尝试过将“数据”放在指令的范围内,而不是从属性中取出?