从结果中取消关闭模式

时间:2015-11-24 15:43:26

标签: javascript angularjs angular-ui-bootstrap

我可以写一些函数来取消结果的结束吗? 错误的想法在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 个答案:

答案 0 :(得分: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');
        };
    }]);

甚至可能不需要注入服务。为什么不简单地在模态控制器中进行验证?

答案 1 :(得分:0)

不,你基本上不能轻易。关闭和解雇是有点终端。你在模态周期中为时已晚。

“我将关闭或不关闭”逻辑正确位置在您的模态控制器内,在“确定”和“取消”方法中。真正的问题是:为什么你不能在这里进行验证?