【问题标题】:How to prevent closing of dialog when data updates fail数据更新失败时如何防止关闭对话框
【发布时间】:2016-11-01 20:29:42
【问题描述】:

我开始使用ngDialog,但我似乎不知道如何防止在数据更新操作期间发生错误时关闭对话框。

        ngDialog.openConfirm({
            scope: $scope,
            template: 'detailsTemplate'
        }).then(
            function () {
                //stop dialog when data insert fails
            },
            function () {
                //cancel clicked
            }
        );

在我看来,这对于任何应用程序来说都是一个非常常见的要求,但我看到的示例都没有解决这个问题。

任何帮助将不胜感激。

【问题讨论】:

  • 你能在你的承诺中实现catch and finally来处理错误吗?我在文档中没有看到任何禁用模式关闭的内容。

标签: angularjs ng-dialog


【解决方案1】:

您将 AJAX 逻辑放在哪里? 您可以在 AJAX 成功后手动关闭对话框,将您的 AJAX 逻辑放入对话框的 Ctrl 而不是 .then 方法中。

小例子供大家参考:

https://jsfiddle.net/ramseyfeng/3spbjpt8/3/

var myApp = angular.module('myApp',['ngDialog']);

myApp.controller('MyCtrl', function ($scope, ngDialog) {
        $scope.names = [];
    $scope.openDialog = function(){
        ngDialog.open({
        scope: $scope,
        template: 'templateForm',
        closeByDocument: false,
        controller: 'PopUpController'
      });
    }
});
myApp.controller('PopUpController', function($scope, ngDialog){
    $scope.cancel = function(){
    $scope.closeThisDialog();
  };
  $scope.submit = function(){
    $scope.disable = true;
    //AJAX logic can be put here
    $scope.names.push($scope.varName);

    //Do not call below sentence if AJAX is not success 
    $scope.closeThisDialog('cancel');


    $scope.disable = false;
  };
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 2018-02-20
    • 1970-01-01
    • 2022-01-06
    • 2014-06-17
    相关资源
    最近更新 更多