NicoJuicy
NicoJuicy

Reputation: 3528

ModalService $on & $broadcast multiple instances

These are the methods in the same angular controller.

The problem is that the $on is called every n clicks. So if it's the 3rd time i clicked to show the popup. The $on will run 3 times and the $broadcast once.

I'm trying to kill the $scope, but it's not working. It seems the controller ( which is also initialised in the dialog, is not getting destroyed.

   ModalService.showModal({
                templateUrl: "ProductPopup.html",
                controller: "ShopActionDialogController",
                inputs: {
                    CurrentProduct: response.data[0]
                }
            }).then(function (modal) {
                 modal.element.modal();
                 args = {};
                 $rootScope.$broadcast('init', args);
              //  $rootScope.$broadcast('init', args);
                modal.element.on('hidden.bs.modal', function (e) {
                  //  modal.element.remove();
                });
                modal.close.then(function (result) {
                    //modal.element.remove();
                    //ModalService.closeModals();

                });
            });

        });

    }

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

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

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

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

Any thoughts?

Upvotes: 2

Views: 146

Answers (0)

Related Questions