问题描述
我正在写一个angularJS应用程序。在这个特殊的控制器,我打开通过 $ window.open
服务一个新的浏览器窗口。但在新的窗口中,所有的 $范围
变量都将丢失。
I'm writing an angularJS app. In this particular controller, I open a new browser window through the $window.open
service. But in the new window, all the $scope
variables are lost.
我试图用 window.parent
但这不起作用。事实上,在新的浏览器窗口中的所有应用程序服务,控制器或范围都没有效果都没有,这是真的吗?有没有办法打开一个新的浏览器窗口,但仍让新的窗口属于在旧的窗口相同的角应用程序?我需要有访问某些angularJS服务和打开新窗口控制器的范围。这是可能的呢?
I tried to use window.parent
but this doesn't work. In fact in the new browser window all the app services, controllers, or scopes are not in effect at all, is this true? Is there a way to open a new browser window yet still makes the new window belongs to the same angular app in the old window? I need to have access to some angularJS services and the scope of the controller that open that new window. Is this possible at all?
推荐答案
有没有[简单]的方法,使新的窗口属于同一angular.js应用,因为应用程序的角度,一般绑在文档,窗口,以及它要么通过NG-应用指令或致电angular.bootstrap初始化窗口的事件。
There is no [straightforward] way to make the new window belong to the same angular.js application, since the angular application is generally tied to the document, window, and events of the window where it was initialized either via an ng-app directive or by calling angular.bootstrap.
不过,您可以为您的弹出窗口一个新的角度模块和应用。您可以通过包括适当的JavaScript文件使用从原始应用程序相同的基本服务和控制器。
However, you can create a new angular module and application for your popup window. You could use the same basic services and controllers from your original application by including the appropriate javascript files.
presuming您的弹出窗口从原来的应用程序需要的数据,可以传递通过窗口对象从window.open()方法返回。例如,你的第一个角应用程序中,打开弹出窗口,如下所示:
Presuming your popup window needs data from the original application, you can pass that by using the window object returned from the window.open() method. For example, within your first angular application, open your popup window like so:
angular.module('originalModule').service('popupService', function(someOtherDataService) {
var popupWindow = window.open('popupWindow.html');
popupWindow.mySharedData = someOtherDataService.importantData;
});
在您的辅助,弹出角初始化应用程序,它可以然后通过读window.mySharedData引用共享数据。
Once your secondary "popup" angular application is initialized, it can then reference the shared data by reading window.mySharedData.
您也可以在每个可从其他窗口被调用窗口创建函数。弹出窗口可以使用window.opener财产回调到原来的窗口。 (window.parent指帧的父窗口,你想window.opener)
You can also create functions in each window that can be called from the other window. The popup window can call back into the original window by using the window.opener property. (window.parent refers to the parent window of frames, you want window.opener)
[商定,我敢肯定,如果你学的角度源$ C $ C你会发现使用相同的应用程序的角度对Windows的一些聪明的办法。这种尝试是超出了我的志向今天晚上。]
[Agreed, I'm sure that if you studied the angular source code you could find some clever way to use the same angular app for both windows. Such an attempt is beyond my ambition this evening.]
这篇关于AngularJS:打开一个新的浏览器窗口,但仍保留范围和控制器和服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!