无法成功建立一连串的承诺

时间:2017-11-13 15:44:53

标签: javascript html node.js angular promise

我向你提出一个问题,尽管付出了很多努力,但我无法解决。

首先,我提供一个插入密码的对话框,然后立即转向控制器并在服务器上执行操作。

我想要做的是获得答案并将其返回到宣布对话框的Callback。

看看这个:

function passwordDialog(packagePath,folderUnzippingTo) {
        return $q(function(resolve, reject) {
            $mdDialog.show({
                controller: passwordDialogController,
                controllerAs: '$ctrl',
                locals: {
                    packagePath: packagePath,
                    folderUnzippingTo: folderUnzippingTo,
                },
                template:
                `<md-dialog  aria-label="Enter Password" layout="column">
                        <md-content md-theme="docs-dark" layout-padding>
                            <md-input-container class="md-block">
                                <label>Password</label>
                                <input type="password" ng-model="password"></input>
                            </md-input-container>
                        </md-content>
                        <md-dialog-actions>
                            <md-button ng-click="sendData(packagePath,folderUnzippingTo)" class="md-raised md-primary">
                            OK
                            </md-button>
                            <md-button ng-click="close()" class="md-primary">
                            Cancel
                            </md-button>
                        </md-dialog-actions>
                    </md-dialog>`,
                // clickOutsideToClose: true,
                parent: $rootScope.parentEl,
            })
                .then(
                function (success) {
                    resolve(success);
                },
                function (err) {
                    console.log("Password Dialog is not working");
                    reject(err);
                }
                );
        });
    }

我希望从服务器获得响应,并将其传递给此处的承诺。

我的控制员:

function passwordDialogController($scope, $mdDialog, packagePath,folderUnzippingTo) {
            return $q(function(resolve, reject) {
                $scope.packagePath = packagePath;
                $scope.folderUnzippingTo = folderUnzippingTo;

                $scope.close = function () {
                    $mdDialog.cancel();
                }

                $scope.sendData = function (packagePath, folderUnzippingTo) {
                    $mdDialog.hide(
                        {
                            "password": $scope.password,
                        });

                    $http.post(SERVER_PATH + '/openPassword', { filePath: packagePath, password: $scope.password,outputFolder: DataFolder.DATA_FOLDER + _PACKAGES_FOLDER + folderUnzippingTo})
                        .then(
                        function (success) {
                            console.log("The password is right");
                            resolve(success);
                        },
                        function (err) {
                            console.log("The password is wrong");
                            console.log("Password controller is not working");
                            reject(err);
                        });
                }
            });
        }

正如我所解释的那样,在收到服务器的回复后,我想回复passwordDialog函数的回调,但因为我没有很好地构建代码,所以我总是在这里取得成功:

              .then(
                function (success) {
                    resolve(success);
                },

一些帮助?

0 个答案:

没有答案