AngularJS确认和承诺 [英] AngularJS validation and promises

查看:213
本文介绍了AngularJS确认和承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个角的应用程序,形式通过自定义功能的JS,然后提交验证。
基于一个条件,我需要显示确认对话框给用户,等待确认或拒绝,然后验证进行。
我在与异步实现这一目标的困难。我相信我没有正确使用的承诺,但不知道需要更新的东西。

In an Angular app, form is validated via custom JS function before submitting. Based on a condition, I need to show a confirmation dialog to the user, wait for confirmation or rejection, then proceed with validation. I'm having difficulties with achieving this asynchronously. I believe I am not using the promise correctly, but not sure what needs to be updated.

工厂

app.factory("confirmDialogService", ['$q', 'ngDialog',
  function ($q, ngDialog ) {
  return {
    popConfirm: function () {
        var deferred = $q.defer();
        var confirmed;
        setTimeout(function () {
                ngDialog.openConfirm({
                    template: 'template/confirmDialog.html',
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    console.log('Modal promise resolved. Value: ', value);
                    deferred.resolve(confirmed =  true);
                }, function (reason) {
                    console.log('Modal promise rejected. Reason: ', reason);
                    deferred.reject(confirmed =  false);
                });
        }, 1000);
        return deferred.promise;
      }
  };
}]);

控制器

  $scope.checkSomething = function (){
    if(needsConfirmation)
    {
        console.log('about to pop confirm');
        confirmationService.popConfirm().then(function(confirmed){
            console.log( 'from popConfirm() ' + confirmed + ''); // never logged
            return confirmed;
        }, function(){
        // something went wrong
            return false;
        });
    }
    else
        return true;
}

确认对话框显示,但点击yes或no的对话框中没有按预期产生结果。

The confirmation dialog is shown, but clicking yes or no in the dialog does not produce results as expected.

我想什么,能够做的就是根据用户是否确认或驳回的对话框中,沿

What I'd like to be able to do is get a true/false value depending whether a user confirmed or dismissed the dialog, along the lines of

var canProceed = $scope.checkSomething();

我想我需要包装成其他承诺,但不知道如何。
任何帮助将大大AP preciated。

I think I need to wrap this into another promise but not sure how. Any help will be greatly appreciated.

谢谢,

推荐答案

由于 openConfirm 已返回一个承诺,你不需要再创建一个与 $ q.defer()

Since openConfirm already returns a Promise, you don't need to create one more with $q.defer():

app.factory("confirmDialogService", ['$q', 'ngDialog', function ($q, ngDialog) {
    return {
        popConfirm: function () {
            return ngDialog.openConfirm({
                template: 'template/confirmDialog.html',
                className: 'ngdialog-theme-default'
            }).then(function (value) {
                console.log('Modal promise resolved. Value: ', value);
                return true;
            }, function (reason) {
                console.log('Modal promise rejected. Reason: ', reason);
                return false;
            });
        }
    };
}]);

之后 checkSomething 会用这种方式:

$scope.checkSomething = function () {
    if (needsConfirmation) {
        return confirmationService.popConfirm().then(function (confirmed) {
            return confirmed;
        }, function () {
            return false;
        });
    } else {
        return $q.when(true);
    }
}

请注意它是如何不再返回真正,而是一个承诺对象。最后,你应该使用 $ scope.checkSomething()像承诺异步操作:

Note how it no longer returns true or false, but rather a promise object. Finally you should use $scope.checkSomething() like asynchronous operation with promises:

$scope.checkSomething().then(function() {
    // ok, proceed
}, function() {
    // something failed
});

汇总,了解它的 checkSomething 应该返回一个承诺,最重要的事情不只是真/假。

Summarizing, the most important thing to understand it that checkSomething should return a promise and not just true/false.

这篇关于AngularJS确认和承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆