从结果中取消关闭模态

Cancel closing modal from result

我可以写一些函数来取消关闭结果吗? 在 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');
        };
    }]);

为什么不在您的 ModalInstanceCtrl 中调用一个服务来检查 "something is wrong",然后再实际关闭它? 因此,您可以在实际关闭它时,传递给 "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');
        };
    }]);

并且注入服务甚至可能不是必需的。为什么不直接在模态控制器中进行验证?

不,你基本上做不到。 close 和 dismiss 是一种终端。你在模态循环中来得太晚了。

"will i close or not" 逻辑正确的位置在你的模态控制器中,在 "ok" 和 "cancel" 方法中。真正的问题是:为什么不能在这里进行验证?