点击我的按钮后如何触发手表?

时间:2014-11-27 11:08:48

标签: angularjs

当我按下此按钮时,如何在按钮上执行$watch()并触发$watch()

HTML:

<button  style="margin-left:10px;" class="btn btn-danger btn-xs">
                    Re-init tableau
                </button>

JS:

scope.$watch('[]', function () {}, true);

1 个答案:

答案 0 :(得分:2)

您可以在按钮上添加ngClick

<button ng-click="addWatch()" style="margin-left:10px;" class="btn btn-danger btn-xs">
                    Re-init tableau
</button>

并在您的控制器中:

$scope.addWatch = function(){
    if( !$scope.isWatchAdded ) {
      $scope.$watch('[]', function () {}, true);    
      $scope.isWatchAdded = true;
    }
}

这将只添加一次watch表达式