javascript - AngularJS:打开一个新的浏览器窗口,但仍然保留范围和 Controller 以及服务

标签 javascript angularjs angularjs-scope angularjs-service angularjs-controller

我正在编写一个 angularJS 应用程序。在这个特定的 Controller 中,我通过 $window.open 服务打开一个新的浏览器窗口。但在新窗口中,所有 $scope 变量都丢失了。

我尝试使用window.parent,但这不起作用。事实上,在新的浏览器窗口中,所有应用程序服务、 Controller 或范围根本不起作用,这是真的吗?有没有办法打开一个新的浏览器窗口,但仍然使新窗口属于旧窗口中的同一个 Angular 应用程序?我需要访问一些 angularJS 服务以及打开该新窗口的 Controller 的范围。这可能吗?

最佳答案

没有[直接]方法使新窗口属于同一个 angular.js 应用程序,因为 Angular 应用程序通常与通过 ng 初始化的窗口的文档、窗口和事件相关联-app 指令或通过调用 angular.bootstrap。

但是,您可以为弹出窗口创建新的 Angular 模块和应用程序。您可以通过包含适当的 javascript 文件来使用原始应用程序中相同的基本服务和 Controller 。

假设您的弹出窗口需要来自原始应用程序的数据,您可以使用从 window.open() 方法返回的窗口对象来传递该数据。例如,在您的第一个 Angular 应用程序中,打开弹出窗口,如下所示:

angular.module('originalModule').service('popupService', function(someOtherDataService) {

  var popupWindow = window.open('popupWindow.html');
  popupWindow.mySharedData = someOtherDataService.importantData;

});

一旦您的辅助“弹出” Angular 应用程序初始化,它就可以通过读取 window.mySharedData 来引用共享数据。

您还可以在每个窗口中创建可以从其他窗口调用的函数。弹出窗口可以使用 window.opener 属性回调到原始窗口。 (window.parent指frames的父窗口,你要window.opener)

[同意,我确信如果您研究了 Angular 源代码,您可以找到一些巧妙的方法来为两个窗口使用相同的 Angular 应用程序。这样的尝试超出了我今晚的野心。]

关于javascript - AngularJS:打开一个新的浏览器窗口,但仍然保留范围和 Controller 以及服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21519113/

相关文章:

AngularJS : Initializing isolated scope inside a directive

javascript - react native 。在 react 导航中隐藏 BottomTab.Navigator 中的项目

javascript - 将变量传递给函数 - Angular

javascript - 重定向到不同域中的相同路径

AngularJS、.NET Web API 和在 SPA 中返回 View 时的后端授权

javascript - 禁用 Angular Directive(指令)模板中的复选框

javascript - $location.search() - 如何在 $scope.$watch 函数内使用范围内的参数?

javascript - Angular 异步调用

javascript - JS : KeyShortcut to focus input - putting character

javascript - Angular.js ng-style 不会绑定(bind)值