AngularJS $ modal里面有一个承诺

时间:2019-01-11 09:21:34

标签: javascript angularjs modal-dialog

所以我有2个模态,第一个模态必须创建一个Booking,在它的内部有第二个模态,用于添加用户。我已经制作了两个模态,并且一个正在为用户使用的模态,但是现在我需要将来自用户添加模态的数据传递给父级(预订模态)。问题在于,我正在其中使用一种服务来添加用户,因此它看起来像这样:

$modal.open({
                        animation: true,
                        backdrop: true,
                        dialogFade: false,
                        keyboard: true,
                        templateUrl: 'templates/main/modals/modalCreateCustomer.html',
                        controller: function ($scope, $modalInstance) {
                            $scope.loading = false;
                            $scope.salutations = self.getSalutations({id: 1});
                            utilsManager.getCountries(function (countries) {
                                $scope.country = {
                                    availableOptions: countries,
                                    selectedOption: countries[79]
                                };
                            });
                            $scope.inlineAlert = {
                                type: "",
                                showDismissButton: true,
                                autoDismiss: true,
                                dismissTime: 5,
                                message: ""
                            };

                            $scope.ok = function () {
                                var data = {
                                    salutation: $scope.salutations.selectedOption.id,
                                    countryId: $scope.country.selectedOption.id,
                                    email: $scope.customer.email,
                                    fullName: $scope.customer.fullName,
                                    phone: $scope.customer.phone
                                };
                                authManager.registerUser(function (response) {
                                    if (!response || !response.errors || response.errors.length) {
                                        //general error
                                        $scope.loading = false;
                                        $scope.inlineAlert.showAlert('error', $translate.instant('registration.error'));
                                    } else {
                                        //$scope.customer.id = response.data.userId;
                                        $scope.loading = false;
                                        $scope.inlineAlert.showAlert('success', $translate.instant('registration.success'));
                                        $timeout(function () {
                                            $scope.customer.email = response.data.email;
                                            $modalInstance.close();
                                        }, 1500);
                                    }
                                }, data);
                            };

                            $scope.cancel = function () {
                                $modalInstance.dismiss('cancel');
                            };
                        }
                    }).result.then(function (data) {
                        $scope.loading = true;
                    });

这是addBooking模态内的添加用户模态。 authManager如下所示:

this.registerUser = function (callback, data) {
        restManager.jsonRequest(constants.get('httpMethods.post'), constants.get('login.register'), function (response) {
            if (callback) {
                callback(response);
            }
        }, data);
    };

我知道模态不等待响应,但是如何使其等待?另外,我可以感觉到我的代码有点像意大利面条,所以对如何更好地组织代码的任何建议都非常欢迎。谢谢!

0 个答案:

没有答案