gpt4 book ai didi

javascript - 从结果中取消关闭模态

转载 作者:行者123 更新时间:2023-11-30 16:27:26 24 4
gpt4 key购买 nike

我可以写一些函数来取消关闭结果吗?在 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 中的服务来检查是否“有问题”?因此,您可以在实际关闭它时,在“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');
};
}]);

注入(inject)服务甚至可能不是必需的。为什么不直接在模态 Controller 中进行验证?

关于javascript - 从结果中取消关闭模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33897923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com