角度服务模式未正确关闭&多次打电话

时间:2018-02-15 22:45:14

标签: angularjs

在github存储库#225 of angular-service-modal

中提到

但我在$ rootScope上没有$ on,所以这应该不是问题。

(还包括以下代码) 我的模态在关闭/隐藏时被多次调用。例如。 [http://beta.belgianbrewed.com/en/product/budweiser-24x30cl](Example页)

有问题的文件可以在/assets/angular/controllers/ShopActionController.js找到

问题可能出在哪里?

代码调用:ShopActionController.js中的showPopup(ProductId,'BuyImmediate')

select id.itemid, dateadd(day, n.n, t.min_adate) as dte, t.qty
from (select t.itemid, min(adate) as min_adate
      from t
      group by itemid
     ) id join
     numbers n
     on dateadd(day, n.n, t.min_adate) < getdate() outer apply
     (select top 1 t.*
      from t
      where t.itemid = id.itemid and t.date <= dateadd(day, n.n, t.min_adate)
      order by t.date desc
     ) t;

服务

app.controller('ShopActionDialogController', ['$scope', 'CurrentProduct', function ($scope, CurrentProduct) {

$scope.CurrentProduct = CurrentProduct;
console.log("Dialog called and ...");
console.log(CurrentProduct);


//modal.element.modal();

//modal.close.then(function (result) {
//    $scope.message = result ? "You said Yes" : "You said No";
//});
}]);

app.controller('ShopActionController', ['$rootScope', '$scope', '$injector', '$http', '$timeout', 'ModalService', 'EndpointService', 'ImageResizeService', 'hotkeys',
function ($rootScope, $scope, $injector, $http, $timeout, ModalService, EndpointService, ImageResizeService, hotkeys) {

    $scope.Resize = ImageResizeService;

    $scope.Amount;
    $scope.InitAmount;
    $scope.ProductId;
    $scope.ProductVariantId;
    $scope.OrderLineId;

    $scope.isLoading = false;
    $scope.isShown = true;
    //$scope.addedToCart = false; //new - not implemented
    $scope.inCartAmount = 0; //new - not implemented

    $scope.inWishList = false;

    $scope.Change = function (Amount) {
        $scope.Amount += Amount;
    }

    $scope.showPopup = function (ProductId,action) {

        EndpointService.searchProducts(undefined, 0, 1, ProductId).then(function (response) {
            ModalService.showModal({
                templateUrl: "ProductPopup.html",
                controller: "ShopActionDialogController",
                inputs: {
                    CurrentProduct: response.data[0]
                }
            }).then(function (modal) {
                // The modal object has the element built, if this is a bootstrap modal
                // you can call 'modal' to show it, if it's a custom modal just show or hide
                // it as you need to.
                // console.log("test");
                modal.element.modal();

                var args = {
                    Amount: $scope.Amount,
                    ProductId: response.data[0].Id,
                    ProductVariantId: response.data[0].ProductVariantId,
                    OrderLineId: undefined,
                    InitAmount : $scope.InitAmount
                };

                if (action != undefined) {
                    args.Action = action;

                }

                $rootScope.$broadcast('init', args);

                $scope.Amount = $scope.InitAmount;

                modal.element.on('hidden.bs.modal', function (e) {
                  //  $scope.Amount = $scope.InitAmount;
                    console.log("hidden");
                    modal.element.remove();
                 //   close(result, 200);
                });
                modal.close.then(function (result) {
                   // console.log("close");
                    $scope.Amount = $scope.InitAmount;
                    modal.element.remove();

                 //   close(result, 200);
                    //ModalService.closeModals();
                    // $scope.message = result ? "You said Yes" : "You said No";
                });
            });

        });

    }

    $scope.CurrentProduct;// = CurrentProduct;

    $scope.initiated = false;

    var initListener = $scope.$on('init', function (event,args) {
        if (!$scope.initiated) {

            $scope.Amount = args.Amount;
            $scope.InitAmount = args.InitAmount;
            $scope.ProductId = args.ProductId;
            $scope.ProductVariantId = args.ProductVariantId;
            $scope.OrderLineId = args.OrderLineId;

            switch (args.Action)
            {
                case 'BuyImmediate':
                    $scope.AddToCart();
                    break;
                case 'View':
                    break;
            }
        }
    });

    $scope.$on('$destroy', function () {
        console.log("killing this scope");
        initListener();
    });

    $scope.init = function (Amount, ProductId, ProductVariantId, OrderLineId) {
        $scope.initiated = true;

        $scope.Amount = Amount;
        $scope.InitAmount = Amount;
        $scope.ProductId = ProductId;
        $scope.ProductVariantId = ProductVariantId;
        $scope.OrderLineId = OrderLineId;
    }

    $scope.getSpecification = function (Product,SpecificationId, SpecificationName) {
        var Spec = undefined;

        angular.forEach(Product.Specifications, function (elem, i) {
            if (elem.SpecificationCandidate.SpecificationId == SpecificationId || 
                elem.SpecificationCandidate.Specification.Name == SpecificationName) {
                Spec = elem.SpecificationCandidate;
            }
        });

        return Spec;
    }

    $scope.isAddedInCart = function () {
        return $scope.OrderLineId != undefined;
    }

    $scope.PriceInclusive = function (orderline) {
        return orderline.Amount * orderline.Product.Price * 1.21;
    }
    $scope.PriceExclusive = function (orderline) {
        return orderline.Amount * orderline.Product.Price;
    }

    $scope.bindKeys = function () {

        hotkeys.add({
            combo: '+',
            description: 'Add 1 to ammount',
            callback: function () {
                $scope.Amount += 1;
            }
        });

        hotkeys.add({
            combo: '-',
            description: 'Substract 1 from ammount',
            callback: function () {
                if ($scope.Amount > 0) {
                    $scope.Amount -= 1;
                }

            }
        });
    }

    $scope.IncrementWith = function (Amount) {
        var newAmount = $scope.Amount + Amount;
        if (newAmount >= 0) {
            $scope.Amount = newAmount;
        }
    }

    $scope.ChangeSortBy = function (SortBy) {
        console.log("Changing Sort By");
    }

    $scope.ChangePageSize = function (PageSize) {
        console.log("Changing PageSize");
    }

    $scope.GetCart = function () {
        return EndpointService.orderlines;
    }

    $scope.RemoveOrderLine = function ($event) {
        $scope.isLoading = true;
        EndpointService.removeOrderLine($scope.OrderLineId)
            .then(function (response) {

                EndpointService.orderlines = response.data;
                $rootScope.$broadcast('DeleteOrderLine', { OrderLineId: $scope.OrderLineId });
                $scope.isLoading = false;
                $scope.isShown = false;
            });

        $event.preventDefault();
        return false;
    }


    $scope.AddToWishlist = function () {
        //AddToWishlist
        EndpointService.addToWishList($scope.ProductId)
            .then(function (response) {
                //return true;
                $scope.inWishList = true;
            });
        return false;
    }

    $scope.RemoveFromWishlist = function () {
        EndpointService.removeFromWishList($scope.ProductId)
            .then(function (response) {
                //return true;
                $scope.isShown = false;
            });
        return false;
    }


    $scope.PrepareAddOrderLineRequest = function (onlyBulk) {
        if (!onlyBulk || (onlyBulk && $scope.InitAmount == 0 && $scope.InitAmount < $scope.Amount)) {
            var request = EndpointService.UpdateAmountRequest;
            request.OrderLineId = $scope.OrderLineId;
            request.Amount = $scope.Amount;
            request.ProductId = $scope.ProductId;

            return request;

        } else {
            return undefined;
        }
    }

    $scope.FocusAmountInput = function () {
        if ($scope.Amount == $scope.InitAmount) {
            $scope.Amount = "";
        }

    }

    $scope.LeaveAmountInput = function () {
        if ($scope.Amount == "") {
            $scope.Amount = $scope.InitAmount;
        }
    }

    $scope.$on("BroadCastBulkOrders", function () {
        var request = $scope.PrepareAddOrderLineRequest(true);
        if (request != undefined) {
            $rootScope.$broadcast("AddBulkOrder", request);
            $scope.Amount = $scope.InitAmount;
        }
    });

    $scope.AddToCart = function () {

        $scope.isLoading = true;

        var request = $scope.PrepareAddOrderLineRequest(false);

        EndpointService.updateAmount(request)
            .then(function (response) {
                EndpointService.orderlines = response.data;
                $rootScope.$broadcast('BasketChanged');

                angular.forEach(response.data, function (elem, i) {
                    if (elem.ProductId == $scope.ProductId) {
                        $scope.OrderLineId = elem.Id;
                        $scope.inCartAmount = elem.Amount;
                    }
                });

                $scope.Amount = $scope.InitAmount;
                $scope.isLoading = false;
            });
        return false;
    }

    $scope.UpdateOrderLine = function () {
        //$emit required values
        $scope.isLoading = true;

        var request = $scope.PrepareAddOrderLineRequest(false);
        request.isFixed = true;


        EndpointService.updateAmount(request)//$scope.OrderLineId, $scope.ProductId, $scope.Amount, true)
            .then(function (response) {
                angular.forEach(response.data, function (elem, i) {
                    if (elem.Id == $scope.OrderLineId) {
                        $rootScope.$broadcast('ChangeAmount', { OrderLineId: $scope.OrderLineId, Amount: $scope.Amount });
                    }
                });


                $scope.isLoading = false;
            });

        return false;
    }
}]);

1 个答案:

答案 0 :(得分:0)

您需要将引导程序模式隐藏侦听器添加到对话框控制器上下文中。

app.controller('ShopActionDialogController', ['$scope', '$element', 'CurrentProduct', 'close', function ($scope, $element, CurrentProduct, close) {

$scope.CurrentProduct = CurrentProduct;
console.log("Dialog called and ...");
console.log(CurrentProduct);


//modal.element.modal();

//modal.close.then(function (result) {
//    $scope.message = result ? "You said Yes" : "You said No";
//});

//listen for when the modal is dismissed and resolve the ModalService Close promise
$element.on('hidden.bs.modal', function (e) {

    close({
            currentProduct: CurrentProduct
        }, 200); // close, but give 200ms for bootstrap to animate
    });

}]);

请注意,将$ element和close添加到了控制器依赖项中。