Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 跨文档/窗口AngularJS应用程序_Javascript_Angularjs_Google Chrome App - Fatal编程技术网

Javascript 跨文档/窗口AngularJS应用程序

Javascript 跨文档/窗口AngularJS应用程序,javascript,angularjs,google-chrome-app,Javascript,Angularjs,Google Chrome App,我正在构建一个打包的chrome应用程序,并希望使用Angular。作为一个具有与本机应用程序相似的ui行为的本地应用程序,我希望使用多个窗口来显示弹出窗口和不同的工作流。与对话框或模态相比,这对用户来说更直观 是否有一种在这些窗口之间共享服务实例的好方法?有没有其他我没有看到的解决方案 更新 更清楚一点,我想在javascript窗口之间共享数据。有一个基本的窗口。postMessage,但我希望能够在这两个单独的窗口之间共享服务实例 可能的解决方案 我发现了一个共享服务的黑客,并在下面发布了

我正在构建一个打包的chrome应用程序,并希望使用Angular。作为一个具有与本机应用程序相似的ui行为的本地应用程序,我希望使用多个窗口来显示弹出窗口和不同的工作流。与对话框或模态相比,这对用户来说更直观

是否有一种在这些窗口之间共享服务实例的好方法?有没有其他我没有看到的解决方案

更新

更清楚一点,我想在javascript窗口之间共享数据。有一个基本的
窗口。postMessage
,但我希望能够在这两个单独的窗口之间共享服务实例

可能的解决方案

我发现了一个共享服务的黑客,并在下面发布了一个答案。我很想考虑一下。更理想的情况是,我可以共享
$injector
服务本身。这可能会考虑到所有可能的选项,并使其看起来就像在同一个应用程序中一样。我相信,
$injector
引用了DOM中的rootElement,这在新窗口中当然会有所不同,因此解决方案可能无法工作。想法?

Angular不提供任何跨窗口支持或任何对设计本地应用程序特别有用的东西。事实上,这并不需要,尤其是对于Chrome应用程序

查看Google为Chrome应用程序提供的localStorage包装,它提供了一些额外的便利(例如在线同步):。您不能像在“普通”web应用程序中那样使用
window.localStorage
,因此即使您只需要基本的localStorage,也需要使用它

文档有点稀疏,但您可以在此处找到有关其用法的教程,包括角度示例代码:

最后,明智的做法是通过一个或多个服务管理此存储,就像您倾向于对AJAX调用/外部数据资源进行管理一样,以便在控制器之间进行封装和重用

更新:

根据@ XAN的注释,您也可以考虑文件系统API,它将为您的应用程序提供

的能力。 创建、读取、导航和写入用户文档的沙盒部分 本地文件系统

根据应用程序的需要,这在处理较大的数据块时可能非常有用,尤其是在与对象存储和
$watch
ers和/或
$interval
轮询相结合以检测应用程序实例之间的更改时


更多信息来自

我已经想出了一种跨多个窗口共享服务的方法,但它仍然有点笨重,如果有更好的解决方案,我会更喜欢

在作为chrome应用程序的父窗口中,您将使用以下代码启动一个新窗口

  chrome.app.window.create("view2.html", options, function(window){

    //This function is a callback that is called when the child 
    //window has been created and launched.
    //I believe it has first access to the window object before any child code
    //Note that the parameter is not the true javascript window but only 
    //a chrome wrapper object. The window is window.contentWindow

    //We are going to place an object on the window that the child code will look for
    window.contentWindow.sharedDependencies = {
       shared_dependency: dependency,
       other_shared_dep: dependency2
    }
  });   
然后在加载第二个页面时,它可以检查窗口上的属性
sharedDependencies
,并适当地加载它们

//The main module must have the dependency module
angular.module("main_app", ["dependancyModule"]);

var deps = angular.module("dependancyModule", []);

if(typeof window.sharedDependencies === "object")
{
   for(var name in sharedDependencies)
      //Register each dependency as a value because we are receiving 
      //it as a value- not as a factory method or a service method. 
      deps.value(name, sharedDependencies[name]);
}
我在一个打包的应用程序中尝试了这一点,但效果很好,但是您必须手动调用$apply(),因为在一个上下文中更改某些内容不会提醒另一个上下文。我还没有想到一个优雅的解决办法


我也是Angular的初学者,所以这可能不支持Angular的其他方面。除了对共享对象的简单绑定之外,我还没有测试过它

作为一种存储选项,还有一件事需要提及。虽然没有其他浏览器会实现它,但Chrome将其作为应用程序的API进行宣传。这是一个很好的答案,但不是我要问的。我正在寻找在单独的javascript窗口对象之间共享角度服务。使用chrome应用程序api,
chrome.app.window.create
,我可以创建一个新的弹出窗口,但我想在这两个窗口之间进行角度通信。当然有
window.postMessage
,但对于我想要的通信级别来说,它不起作用。