2017-07-26 51 views
0

我正在創建一個客戶端並調用我的API,但它至少需要2秒才能響應,因此我希望該用戶無法單擊兩次註冊在角度js中強制單擊

我將這個小提琴放入我的控制器中。

https://jsfiddle.net/zsp7m155/

但什麼情況是,第一個函數需要2秒鐘禁用按鈕之後,它能夠使他按鈕併發送請求給API。 我在這裏做錯了什麼?

代碼我我的控制器

CONTROLLER.JS

$scope.createClient = function() { 


     ClientService.createClient($scope.theClient).then(function (aCreateClientResponse) { 
      if(aCreateClientResponse[0] == $scope.RESPONSE_CODE.CM_SUCCESS) {     
       alert('success');    
      } 
      else if(aCreateClientResponse[0] == $scope.RESPONSE_CODE.CM_DOMAINNAME_ERROR) { 
       alert('Check your domain name');    
      } 
      else if(aCreateClientResponse[0] == $scope.RESPONSE_CODE.CM_INVALID_INPUT) { 

       alert('Invalid request');    
      } 
      else { 

       alert('Service is not available');   
      } 
     });   
}; 

代碼我服務的

SERVICE.JS

App.factory('ClientService', function($http, API_URL, REQUEST_HEADER, RESPONSE_CODE) { 

     createClient: function(aClient) {  

     var myCreateClientRequest = { 
      "CreateClientRequest": { 
       "Header": { 
        "CMMHeader": { 
         "Id": uuid2.newuuid() 
        } 
       }, 
       "Client": { 
        "OrganizationName": aClient.Name, 
        "OrganizationDomain": aClient.Domain, 
       }, 
      } 
     }; 

     //API Call 
     var promise = $http.post(API_URL.CREATE_CLIENT, myCreateClientRequest, REQUEST_HEADER).then(
     function(aCreateClientResponse) { //Success Callback 

      return [aCreateClientResponse.data.CreateClientResponse.Result.ResponseCode,'']; 
     }, 
     function(aCreateClientResponse) { //Error Callback 

      return [aCreateClientResponse.status,'']; 
     }); 

     return promise; 
    }, 

       }); 

HTML

<button id="register-btn" name="register-btn" class="btn btn-primary" ng-disabled="((Client.User.UserPassword !== Client.User.UserconfirmPassword) || CreateClientForm.domain.$invalid || CreateClientForm.username.$invalid || CreateClientForm.email.$invalid || CreateClientForm.password.$invalid || CreateClientForm.confirmpassword .$invalid || CreateClientForm.organizationname.$invalid)" single-click="createClient()">{{ running ? 'Please wait...' : 'Register' }}</button> 
+0

我已經使用他來驗證我的支票 <按鈕的ID

<button id="register-btn" name="register-btn" class="btn btn-primary" ng-disabled="isRegistering" single-click="createClient()">{{ running ? 'Please wait...' : 'Register' }}</button> 

,並在控制器= 「寄存器BTN」 NAME = 「註冊-BTN」 class =「btn btn-primary」ng-disabled =「((Client.User.UserPassword!== Client.User.UserconfirmPassword)|| CreateClientForm.domain。$ invalid || CreateClientForm.username。$ invalid || CreateClientForm.email 。$ invalid || CreateClientForm.password。$ invalid || CreateClientForm.confirmpassword。$ invalid || CreateClientForm.organizationname。$ invalid)「single-click =」createClient()「> {{running? '請稍候...':'註冊'}} –

+0

[防止/處理角度中的雙擊按鈕]可能的副本(https://stackoverflow.com/questions/18130808/preventing-dealing-with-double- button-clicks-in-angular) – tk120404

回答

2

你有幾個選項,最簡單的就是,當你打電話給你的API,當它解決了重新啓用禁用按鈕。你可以做這樣的:

$scope.isRegistering = false; 

$scope.createClient = function() { 

    $scope.isRegistering = true; 
    return $timeout(function() { 
     ClientService.createClient($scope.theClient).then(function (aCreateClientResponse) { 
      $scope.isRegistering = false; 
      if(aCreateClientResponse[0] == $scope.RESPONSE_CODE.CM_SUCCESS) {     
       alert('success');    
      } 
      else if(aCreateClientResponse[0] == $scope.RESPONSE_CODE.CM_DOMAINNAME_ERROR) { 
       alert('Check your domain name');    
      } 
      else if(aCreateClientResponse[0] == $scope.RESPONSE_CODE.CM_INVALID_INPUT) { 

       alert('Invalid request');    
      } 
      else { 

       alert('Service is not available');   
      } 
     }, function(){ 
      $scope.isRegistering = false; 
     });   
    }, 1000); 
}; 
+0

我已經在其中使用了ng禁用功能。檢查更新後的答案

+0

怎樣才能對功能進行檢查? –

+1

您可以添加'||正在註冊「到您現有的禁用功能,並應該工作。當記得將相應的isRegistering-variable設置爲true/false時。您可以在我的答案中使用控制器以獲取靈感,以此作爲實現它的一種方式。 –