2015-04-03 86 views
0

我試圖在我的角度應用程序中使用引導模式服務來實現警報/確認對話框。我想根據傳遞給模態控制器的參數將其設置爲通用的,它將作爲警報框,其中用戶可以在閱讀模式上的消息時關閉它,也可以像確認對話框那樣工作用戶應該說'確定'或'取消'我需要捕捉來自用戶的響應。現在,這裏的問題是,我有一個項目列表在網格中說,當用戶想要從列表中刪除一個項目時,我需要顯示確認消息框,並根據用戶響應我需要刪除項目或者如果用戶希望將它留在列表中,則將其保留,但是我的項目正在被刪除,然後確認對話框顯示出來,我嘗試使用回調但仍然沒有用。如果有人遇到這種情況,請幫助我。警報和確認對話框AngularJs

方法,顯示了警報:

$scope.showAlertMessage = function (modalName,commands,callback) 
    { 
     var modalOptions = {}; 
     var alertMessageText;  
     var okBtn; 
     var cancelBtn; 
     var autoCloseTimeout;   
     $scope.modalResp === false; 

     if (modalName === 'ItemNotEligible') 
     { 
      modalOptions['template'] = 'application/Views/ItemNotEligibleAlert.html'; 
      modalOptions['cntrlr'] = 'itemAlertsController'; 
      modalOptions['winClass'] = 'item-alert-win'; 
      alertMessageText = commands.alertMessage.text;    
      okBtn=true; 
      cancelBtn = false; 
      autoCloseTimeout=commands.alertMessage.autoDismissalTimeout; 

     } 
     else if (modalName === 'ItemDelete') 
     { 
      modalOptions['template'] = 'application/Views/ItemNotEligibleAlert.html'; 
      modalOptions['cntrlr'] = 'itemAlertsController'; 
      modalOptions['winClass'] = 'item-alert-win'; 
      alertMessageText = commands.alertMessage.text;    
      okBtn = true; 
      cancelBtn = true; 
      autoCloseTimeout = commands.alertMessage.autoDismissalTimeout; 
     } 

     var params = { alertMessage: alertMessageText}; 

     var modalInstance=$modal.open({ 
      templateUrl: modalOptions.template, 
      controller: modalOptions.cntrlr, 
      windowClass: modalOptions.winClass, 
      resolve: { 
       modalParams: function() { 
        return params; 
       } 
      } 
     }); 

     modalInstance.result.then(function (selected) { 
      $scope.modalResp = selected; //Response from the dialog 
     }); 
     callback($scope.modalResp); 
    } 

方法,其中刪除項邏輯存在,並呼籲以示警戒方法由

this.voidItem = function (skuid) { 

     alertMessage.text = 'Are you sure you want to remove <strong>' + itemdata[itmid].split('|')[0] + '</strong> from your list?'; 

     $scope.showAlertMessage('ItemDelete', commands, function (userresp) { 
      if (userresp === true) { 
       var lineId = 0; 

       for (var i = itemListState.length - 1; i >= 0; i--) { 
        if (parseInt(itemListState[i].item) === itmid && Boolean(itemListState[i].isItemVoid) != true) { 
         lineId = itemListState[i].Id; 
         if (lineId != 0) { 
          break; 
         } 
        } 
       } 

       poService.DeleteItem(itmid, lineId, function (modal) { 
        virtualtable = modal; 
        $scope.itemCount = $scope.itemCount - 1; 
        $scope.createlist(itmid); 
       }); 
      } 
     }); 
    } 

回答

1

的問題是,你正在執行的回調,而不是將該方法鏈接到承諾的結果

modalInstance.result.then(function (selected) { 
     callback($scope.modalResp); //Once the promise is solved, execute your callback 
     $scope.modalResp = selected; //Why you need to save the response? 
    // If the user press cancel o close the dialog the promise will be rejected 
    }, onUserCancelDialogFn);   

更簡單的w ay:

modalInstance.result 
    .then(callback, onErrBack); 
+0

真棒馬託!!!謝謝。這是我正在尋找的。 – 2015-04-06 16:01:18