代码之家  ›  专栏  ›  技术社区  ›  Jose

在两个窗口之间共享变量。棱角

  •  0
  • Jose  · 技术社区  · 5 年前

    在angularjs应用程序中,我打开一个新窗口:

     $window.open($state.href('catalog-detalleTicketMapa', {
          id: $localStorage.idRamdom
        }), 'window', 'height=700,width=700');
    

    当我关闭这个新窗口时,我想让父窗口知道这个事件,我做了以下操作

    // controller auxiliary window
    vm.onExit = function () {
      $rootScope.$broadcast('ventanaCerrada');
    };
    
    $window.onbeforeunload = vm.onExit;
    
    // controller main windows
    $scope.$on('ventanaCerrada', function () {
      $rootScope.ventanaAbierta = false;
    });
    

    如果它进入事件$rootScope.$广播,但不进入事件作用域。$打开。

    我也用localStorage进行了测试,但是它只在辅助窗口中更新,而主窗口没有。

    如何在浏览器中同时在两个打开的窗口之间共享变量?

    2 回复  |  直到 5 年前
        1
  •  1
  •   Florian Lim    5 年前

    $window.open

    我有以下代码 WindowService 要打开新窗口(我们只打开一个窗口;如果需要更多窗口,则需要调整代码):

    angular.module('myproject')
    .service('WindowService', ['$window', '$interval', '$timeout', 
    function ($window, $interval, $timeout) {
    
        var self = this;
        this.myOpenWindow = null;
    
        // opens a window and starts the wait-function
        this.openWindow = function (url, callbackFn) {
            // open the window
            self.myOpenWindow = $window.open(url);
    
            // Puts focus on the new window
            if (window.focus) {
                self.myOpenWindow.focus();
            }
    
            // check for response in opened window
            self.waitForResponse(callbackFn);
        };
    
        this.waitStarted = false;
    
        this.waitForResponse = function (callbackFn) {
            if (!self.waitStarted && self.myOpenWindow) {
                self.waitStarted = true;
                var intervalPeriod = 1000; // check once every second
                var waitHandler;
                waitHandler = $interval(function (index) {
                    if (self.myOpenWindow.closed) {
                        // window was closed, commence cleaning operation
                        $interval.cancel(waitHandler);
                        waitHandler = $timeout(function () {
                            // re-initialize waitStarted so it can be used again
                            self.waitStarted = false;
                            // call callback-Function when the window is closed
                            if (callbackFn) {
                                callbackFn();
                            }
                        }, intervalPeriod);
    
                    } else {
                        // window is still open, check if the observed variable is set 
                        if (self.myOpenWindow && self.myOpenWindow.response_message) {
                            var message = self.myOpenWindow.response_message;
                            // do what you want with the message
                            // ...
                            // afterwards close the window (or whatever)
                            if (self.myOpenWindow) {
                                self.myOpenWindow.close();
                            }
                        }
                    }
                }, intervalPeriod);
            }
        };
    }
    ]);
    

    在打开的窗口中,您需要在某个地方设置全局变量,例如:

    window['response_message'] = { 'result': 'Done' };
    
        2
  •  0
  •   Mak Sr    5 年前