Angular, disable a button and then activate it using setTimeout in javascript?

时间:2015-09-14 15:19:58

标签: javascript angularjs

I want to disable my button but then activate it a few seconds later. The code does run but the button is still disabled even after my code has executed.

app.controller('spamController', ['$scope', function($scope) {


$scope.stopSpam = false;
  
function activateBtn(){
  $scope.stopSpam = false;
};


$scope.test = function(){
  
  $scope.stopSpam = true;
  activateBtn();

};

}]);
<button ng-disabled="stopSpam" class="btn btn-default" ng-click="test()">Test</button>

1 个答案:

答案 0 :(得分:3)

Use $timeout and call activateBtn after a few seconds:

app.controller('spamController', ['$scope', '$timeout', function($scope, $timeout) {
    $scope.stopSpam = false;

    function activateBtn(){
        $scope.stopSpam = false;
    };


    $scope.test = function(){
        $scope.stopSpam = true;
        $timeout(activateBtn, 3000);
    };
}]);