2015-11-24 19 views
1

我可以寫一些函數來取消結果關閉嗎? 壞主意在ModalInstanceCtrl中保存模型。取消結果模式

app.controller('MainController', ['$scope', '$modal', function ($scope, $modal) { 
     $scope.edit =function (id) { 
      var modal = $modal.open({ 
       templateUrl: 'myModalContent.html', 
       controller: 'ModalInstanceCtrl' 
      }); 

      modal.result.then(function(model) { 
       if (somethink_wrong) { 
        ***CANCEL CLOSING*** 
       } 
      }); 
     }; 
    }]); 

    app.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', function ($scope, $modalInstance) {  
     $scope.ok = function() { 
      $modalInstance.close($scope.model); 
     };  
     $scope.cancel = function() { 
      $modalInstance.dismiss('cancel'); 
     }; 
    }]); 

回答

2

爲什麼不在你ModalInstanceCtrl調用服務,以檢查是否「什麼是錯的」,實際上是關閉之前? 因此,當你真正關閉它時,你可以做到這一點,傳遞給「then」中的匿名函數的模式將是有效的數據,你肯定沒有錯。

app.controller('MainController', ['$scope', '$modal', function ($scope, $modal) { 
     $scope.edit =function (id) { 
      var modal = $modal.open({ 
       templateUrl: 'myModalContent.html', 
       controller: 'ModalInstanceCtrl' 
      }); 

      modal.result.then(function(model) { 
       *There is nothing wrong because we already checked :)* 
      }); 
     }; 
    }]); 

    app.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', yourService function ($scope, $modalInstance, yourService) {  
     $scope.ok = function() { 
      if (yourService.checkNothingWrong()) { 
       $modalInstance.close($scope.model); 
      } 
      else { 
       *inform user something is wrong* 
      } 
     };  
     $scope.cancel = function() { 
      $modalInstance.dismiss('cancel'); 
     }; 
    }]); 

而注入服務可能甚至不是必要的。你爲什麼不直接在模態控制器中進行驗證?

0

不,你基本上不會輕易。關閉和解僱是有點終端。你在模態循環中已經太晚了。

在你的Modal控制器裏面的「確定」和「取消」方法中,「我將關閉或不關閉」邏輯正確的位置。真正的問題是:爲什麼你不能在這裏做你的驗證?