javascript - Angular 模态返回 promise

标签 javascript angularjs promise

我想在用户执行操作(在本例中为删除)之前向用户显示一个弹出式警告模式。如果他们在模式中单击 okay,它将继续,如果他们单击 cancel,它将执行其他操作。

为此,我试图将模态 promise 沿链传递回初始调用 $rootScope.openDirections('warning').then(... 以便我可以决定在那里做什么。

但是我遇到了错误:

$rootScope.openDirections(...).then 不是函数 at Scope.$scope.deleteObj

告诉我 promise 没有被返回:

    $scope.deleteObj = function (id, type) {
        $rootScope.openDirections('warning').then(function (res) {
            // Do Stuff
        }, function (err) {
            console.log('ERROR', err);
        });

这调用了一个 $rootScope 函数:(我看到这里的对象类型是一个 promise...所以它现在不应该能够返回到原始调用者吗?)

    $rootScope.openDirections = function (type) {
        // A Promise: Object {result: Promise, opened: Promise, rendered: Promise}
        return Modal.showDirections(type);
    };

调用 Modal 工厂打开方向模式:

     return {
            ...
            showDirections: function(type) {   
                return $modal.open({
                    backdrop: 'static',
                    templateUrl: 'modal.html',
                    controller: 'directionsCtrl',
                    resolve: {
                        // Display custom modal template
                        obj: function () {
                            if (type === 'markdown') {
                                return {
                                    template: '/modals/directions.html'
                                };
                            } else {
                                return {
                                    template: '/modals/message.html'
                                };
                            }
                        },
                        testID : function () {
                            return 'WHAT DO I RETURN HERE?';
                        }
                    }
                });
            }

呈现 message.html 模态模板:

<div class="admin-directions">
    <div class="directions">
        ARE YOU SURE YOU WANT TO DELETE THIS?
    </div>
    <p class="dismiss-admin-help" ng-click="okay()">Okay</p>
    <p class="dismiss-admin-help" ng-click="cancel()">Cancel</p>
</div>

它使用 directionsCtrl 作为 Modal:

angular
    .module('DDE')
    .controller('directionsCtrl', ['$rootScope', '$scope', '$modalInstance', 'Modal', 'obj', 'Auth', 'VARS', '$route', '$location', 'testID',
        function ($rootScope, $scope, $modalInstance, Modal, obj, Auth, VARS, $route, $location, testID) {

            $scope.template = obj.template;

            $scope.testID = '';

            /**
             * Dismiss modal
             */
            $scope.ok = function() {
                $scope.testID = 'okay';
                return $modalInstance.result($scope.testID);
            };

            $scope.cancel = function() {
                $scope.testID = 'cancel';
                return $modalInstance.result($scope.testID);
            };

    }]);

最佳答案

$modal.open 返回一个 $modalInstance,它有一个名为 result 的属性,它是 promise。你必须调用 .then() 。所以在你的情况下,你想要这个:

    $rootScope.openDirections('warning').result.then(function (res) {
        // Do Stuff
    }, function (err) {
        console.log('ERROR', err);
    });

或者,在您的 openDirections 方法中,您可以只返回 promise :

      showDirections: function(type) {   
            return $modal.open({
               //all your current stuff stays the same here
             }).result; //<-- notice that you are now returning the promise
        }

关于javascript - Angular 模态返回 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954045/

相关文章:

javascript - 使用 Angular 工厂来保存应用程序其余部分访问的值,以最大限度地减少服务器调用

javascript - 使用侧边栏阻止语义 UI 溢出内容

javascript - 如何在 HTML 中将 $scope 变量的值作为字符串提供?

Javascript Promise 和更新变量

javascript - 如何获取ng-repeat输入angularjs的值?

typescript - 如何用 Jest 测试 void 异步函数是否成功?

promise - vertx中的future和promise有什么区别?

javascript - 将 extjs 项动态添加到字段容器

javascript - 是否有预先存在的 Javascript 函数可以将 "2009-09-16T11:10:00"读取为日期?

javascript - jQuery : How to Select input fields which are displayed in tabs