0

I am presenting to you a problem I can not solve despite much effort.

First, I present a dialog to insert a password, and then immediately turns to the controller and executes an action on the server.

What I want to do is get the answer and return it to Callback where the dialog is announced.

look at the this :

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);
                }
                );
        });
    }

I want to get a respond from the server and pass it through a promise to here.

my controller:

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);
                        });
                }
            });
        }

As I explained, after receiving a reply from the server I would like to return a callback to the passwordDialog function but because I did not build the code well I always got success here :

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

some help?

antzshrek
  • 9,276
  • 5
  • 26
  • 43
Tom Cohen
  • 95
  • 1
  • 2
  • 9
  • 1
    According to the docs, `$mdDialog.show` already returns a promise: no need to manually construct one. Same for `$http.post`. You really need to reduce this to a minimal case to reproduce. – Jared Smith Nov 13 '17 at 15:51
  • [Avoid the promise constructor anti-pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) - spotting the anti pattern is easy in this case because you have resolve/reject in a `.then` - this is not your issue, in fact, I can't see why you'd always get success if there was indeed a failure – Jaromanda X Nov 13 '17 at 23:17
  • Even after removing .then from the controller I always get success in the passwordDialog function. – Tom Cohen Nov 14 '17 at 09:58

0 Answers0