3

I am using the ng-disabled directive to control when a button on my page can be clicked. Is it possible to control this via a timer? I would like the button to be disbaled until 5 seconds have elapsed. My HTML is currently:

<div ng-controller="ResetController">
    <button ng-click="reset()">Reset</button>
</div>
3
  • please post your code Commented Apr 1, 2016 at 14:28
  • What is ngEnabled, is it custom directive of you mean ngDisabled? <button ng-disabled="disabled"></button> and $timeout(function() { $scope.disabled = false }, 5000). Commented Apr 1, 2016 at 14:31
  • yes - I should be using ngDisabled. Thanks. Commented Apr 1, 2016 at 14:41

5 Answers 5

13

You can use $timeout:

$timeout(function() {
   $scope.buttonEnabled = true;
}, 5000);

And in your html:

<button ng-click="reset()" ng-disabled="!buttonEnabled">Reset</button>

Also don't forget to inject $timeout into your controller. Like any other angular Service ($http for example).

Sign up to request clarification or add additional context in comments.

Comments

7

ng-enabled isn't in core, but ng-disabled evaluates based on either true or false so simply use a timeout to assign true to a variable after a desired amount of time to enable accordingly.

html:

<button ng-disabled="isDisabled">click me after 3 seconds</button>

controller:

function($scope, $timeout) {
  $scope.isDisabled= true;
  $timeout(function(){
    $scope.isDisabled= false;
  }, 3000)
}

2 Comments

There is no ng-enabled, or am I missing something? Also it should be 5 seconds, not 3.
I've not done angular for a while in favour of react, just realised, and was mid edit as you commented. :)
3

You need to set a variable in your controller, then set ng-disabled using that variable.

For example, assuming a controller ExampleController registered with controller-as "example":

(function() {
    'use strict';
    angular.module('app').controller('ExampleController',ExampleController);
    ExampleController.$inject = ['$timeout'];

    function ExampleController($timeout) {
        var vm = this;
        vm.buttonDisabled = true;
        $timeout(function() { 
            vm.buttonDisabled = false;
        }, 5000);
    }
})();

Then in your template:

<input type="button" ng-disabled="example.buttonDisabled"/>

2 Comments

I think it might be worth the correct vote because of all of the best practices in this answer, not something that is strictly necessary, but beneficial to someone new and starting out.
Thanks @AdamCooper86 - one of the problems I had starting out with Angular was the number of different ways there are to do things, the controller-as syntax and proper isolation of different elements was something that many examples missed but really helps to keep clean, testable, & bug-free code.
1

you need to use timeout as per the previous answer ...

$scope.buttonDisabled = true;

$timeout(function() {
   $scope.buttonDisabled = false;
}, 5000);

don't forget to inject $timeout into your controller / service.

then in your HTML do ...

<div ng-controller="ResetController">
    <button ng-click="reset()" ng-disabled="buttonDisabled">Reset</button>
</div>

Comments

-1

Try this:

var interval= setInterval(timePassed, 1000);
function timePassed() {
   $scope.buttonEnabled = true;
}

and:

<input type="button" ng-enabled="buttonEnabled"/>

1 Comment

This wouldn't work. You'd need to use $interval or $timeout or manually digest

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.