2017-10-13 23 views
2

我有一個ng-repeat項目列表,每個項目都有一個刪除按鈕。刪除按鈕的動作是一些Ajax請求。我的問題是,如果用戶在刪除按鈕上單擊多次,我的api調用將失敗,因爲它在第一次單擊時被刪除,而傳遞ID在第二次單擊中無效。 我想在第一次點擊後取消綁定點擊並重新綁定我的ajax請求成功回調。在第一次點擊後取消綁定元素的單擊事件並在角度js後執行操作後重新綁定它

這是我曾嘗試是

HTML

<li ng-repeat="labels in addedLabels track by $index"> 
    {{labels.Label.label_name}} 
    <button ng-click="removeLabel(labels.label_id, labels.module_id,$index,true,$event);"> 
    </button> 
</li> 

控制器中

$scope.removeLabel = function(label_id,module_id,index,popup,event){ 
    $(event.currentTarget).off('click') 
    opts.url = SERVER_URL + 'path'; 
    opts.data = { 
       label_id: label_id, 
       module_id : module_id 
      }; 
    $http.post(opts.url,opts.data) 
    .then(function(response){ 
     // some action 
    },function(err){ 
     // error action 
    }).finally(function(){ 
     $(event.currentTarget).on('click') 
    }) 

回答

2

disable編輯按鈕,直到刪除API調用響應通往成功或者失敗。也可以通過labels對象到removeLabel方法,這樣可以很容易的從屬性中獲取相同的屬性。

的Html

<li ng-repeat="labels in addedLabels track by $index"> 
    {{labels.Label.label_name}} 
    <button ng-disabled="labels.disabled" ng-click="removeLabel(labels,$index,true,$event);"> 
    </button> 
</li> 

代碼

$scope.removeLabel = function(label, module_id, index, popup, event) { 
    opts.url = SERVER_URL + 'path'; 
    opts.data = { 
     label_id: label.label_id, 
     module_id: label.module_id 
    }; 
    label.disabled = true; //disabling button 
    $http.post(opts.url, opts.data) 
     .then(function(response) { 
     // some action 
    }, function(err) { 
     // error action 
    }).finally(function() { 
     label.disabled = false; //enabling it 
    }) 
} 
+0

你好潘卡, 我已經嘗試過了。但它也刪除了下一個元素。 我已經點擊第一個元素2次,然後第一個和第二個元素被刪除 – Jennifer

+0

@Jennifer可能是錯誤的處理從數組中刪除該標籤的代碼。 – kappaallday

+0

我已編輯我的問題。代碼添加刪除數組中的標籤。 另外,當我把我的$ http請求放在$超時它工作正常。我不確定發生了什麼事 – Jennifer

相關問題