2

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?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
NicoJuicy
  • 3,435
  • 4
  • 40
  • 66
  • If you're in the same controller rather than using $rootScope.$broadcast wouldn't it make more sense to just make your initListener a function and call it from the modal resolve? You really should try to avoid using $rootScope.$broadcast. – init fail Jan 01 '18 at 17:20
  • I'm loading another view, but with the same controller ( same actions). There is a difference, the controller "ShopActionController" is shown in the Product Overview, but also in the popup. Where the same actions are available – NicoJuicy Jan 01 '18 at 17:56
  • what are you using for showModal? angular-bootstrap? – alphapilgrim Jan 01 '18 at 18:32
  • https://github.com/dwmkerr/angular-modal-service – NicoJuicy Jan 02 '18 at 15:17

0 Answers0