0

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 Answer 1

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);
    };
}]);
Sign up to request clarification or add additional context in comments.

4 Comments

if I used plain javascript timeout instead of the $timeout why would it not work?
Because setTimeout doesnt trigger a $digest cycle - which is what updates the view.
Can call $scope.$apply() in setTimeout.
@dustmouse -- You can, but it's considered bad practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.