javascript - Angular $watch 不会被触发

标签 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 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));

然后,在我的 Controller 中我会:

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-click 的按钮,它调用 hideBenchmark 函数。此函数确实被调用,dataTable 版本已更新,但无论我尝试什么, watch 都不会被触发。

我尝试在我的 hideBenchmark 函数中调用 $scope.$appy()/$scope.$digest(),尝试向 watch 添加“true”(寻找对象不平等),尝试观察“attrs.data” , 'data.version' 似乎都不会触发 watch !我知道我可以在范围内创建另一个独立于该数据的变量,观察它并在每次更改数据时更改它,但这看起来很丑陋且不必要。

有谁知道为什么 watch 没有被触发,我怎样才能让它做我想做的事?

问候,

林纳斯

最佳答案

按照 Sunil 的建议,你应该把它放在范围内:

scope: {
    data: '='
} 

因为如果你不这样做,数据就不会在指令中更新。

这里是 fiddle : http://jsfiddle.net/0t4z02yw/2/

关于javascript - Angular $watch 不会被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26732785/

相关文章:

javascript - ng-switch 不自动更新

javascript - 在angularjs中使用来自rest服务的JSON对象

javascript - 将哈希密码发送到 WebAPI

javascript - Angular js 将 'file' 类型更改为 'text' 类型 [Safari]

javascript - 如何从 focusout 中排除 Id

javascript - 简单的 Javascript 数学函数

javascript - 通过 jquery 将图像 id 传递给 php mysql 查询

javascript - 将参数传递给主干 View

javascript - 多个类上的 querySelectorAll 不起作用

javascript - 使用 angular.js 在项目上切换事件类