0

In my angular application, I have some buttons that call REST services.

Sometimes, the Rest Service takes a long time to reply. To prevent the user to click once before receiving the service answer, I would like to disable the button and change his value to 'loading...'

I want to do the same, only for all buttons whose value is equal to 'Search' or 'Relaunch'

Exemple: enter image description here

Code :

<input type="submit" class="btn" ng-click="relaunch()" ng-value="Relaunch"/>

$scope.relaunch= function(){
    RelaunchService.relaunch().then(function(data){
       // server response
      })
};

For this, I want to create one directive. But I do not know how to do it :-(

PS: Note that the user can click on several different buttons at the same time.

2
  • Please, show us your code. Commented Mar 4, 2016 at 10:35
  • @AlessioCantarella code added Commented Mar 9, 2016 at 9:42

1 Answer 1

1

You can use ng-disabled

Syntax is

<INPUT
 ng-disabled="expression">
  ...
</INPUT>


So in your case you can do it as

<input type="submit" class="btn" ng-disabled="btnDisabled"  ng-click="relaunch()" ng-value="Relaunch"/>
 $scope.btnDisabled=false;
 $scope.relaunch= function(){
     $scope.btnDisabled=true;
     RelaunchService.relaunch().then(function(data){
       // server response

     }).finally(function(){
         $scope.btnDisabled=false;
     })
 };
Sign up to request clarification or add additional context in comments.

2 Comments

You probably better have to use finally() instead of then() to re-enable button
Thank you for your answer. I want to use only one directive. because I have a lot of button so a lot of code to modify in this case

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.