Javascript 当摘要循环以角度运行时,防止按钮单击

Javascript 当摘要循环以角度运行时,防止按钮单击,javascript,angularjs,Javascript,Angularjs,我通过点击按钮来更新$scope,这基本上是一个重新启动的调用,获取数据并填充$scope变量 但是,如果用户在运行$digest循环时多次单击该按钮,则$scope将使用重复的值进行更新 我敢肯定这是一个普遍的问题。然而,我还没有遇到一个这样的解决方案,我正在寻找 人们建议使用$timeout,但这似乎违反了逻辑 我宁愿在$digest循环运行时禁用该按钮 以下是我到目前为止所实现的 $scope.names = []; $scope.disableButton = true; FetchNa

我通过点击按钮来更新$scope,这基本上是一个重新启动的调用,获取数据并填充$scope变量

但是,如果用户在运行$digest循环时多次单击该按钮,则$scope将使用重复的值进行更新

我敢肯定这是一个普遍的问题。然而,我还没有遇到一个这样的解决方案,我正在寻找

人们建议使用
$timeout
,但这似乎违反了逻辑

我宁愿在
$digest
循环运行时禁用该按钮

以下是我到目前为止所实现的

$scope.names = [];
$scope.disableButton = true;
FetchNames.getAllNames().then(function(result){
  result.data.forEach(function(name){
    $scope.names.push(name);
  });
$scope.disableButton = false;
});
在这里,当承诺得到解决时,按钮被禁用。 但这并不能解决我的问题。我需要检查摘要周期何时完成。

尝试以下操作:

$scope.names = [];
$scope.disableButton = true;
FetchNames.getAllNames().then(function(result){
  // if in digest; reuturn
  if($scope.$$phase || $scope.$root.$$phase ){return}
  result.data.forEach(function(name){
    $scope.names.push(name);
  });
$scope.disableButton = false;
});

使用ng disabled内置指令在用户单击按钮时禁用该按钮。要实现这一点,只需将变量设置为true。请看下面的代码

<button ng-click="btn1=true, yourFunction()" ng-disabled='btn1'>

我想这对你有帮助


这个

这对你很有用,我想我不明白为什么
$timeout
会是“反逻辑”的,因为它在启动callback.cbass之前等待摘要循环完成,它不会再次运行摘要循环吗?这并不能解决再次启动摘要循环的问题。我发现这对您的摘要循环很有用。希望你能成功!如果你喜欢的话,你能取消否决票和赞成票吗。
    $scope.btn1= false;
    $scope.yourFunction= function(){
      $scope.names = [];
      FetchNames.getAllNames().then(function(result){
        result.data.forEach(function(name){
          $scope.names.push(name);
       });
       $scope.disableButton = false;
     };