本文介绍了是否可以共享两个angularjs应用程序之间的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有相同的页面上有两个不同的应用程序。
I have two different apps on the same page.
我可以通过一个服务共享这两个应用程序之间的数据(或任何其他方式?)?
Can I share data between these two apps via a service (or any other way?)?
或者,这是不可能的?
(我怀疑的是,这是不可能的,虽然规则的角度机制) - 但我认为这仍然是值得问...
(my suspicion is that this is not possible though regular angular mechanisms) - but I thought it would still be worth to ask...
这可以使用窗口变量来实现 - 但我想避免这样做。
This can be done using the window variable - but I want to avoid doing so.
谢谢!
推荐答案我最终解决它以下列方式:
I eventually solved it in the following manner:
angular.module('sharedService', []).factory('SharedService', function() { var SharedService; SharedService = (function() { function SharedService() { /* method code... */ } SharedService.prototype.setData = function(name, data) { /* method code... */ }; return SharedService; })(); if (typeof(window.angularSharedService) === 'undefined' || window.angularSharedService === null) { window.angularSharedService = new SharedService(); } return window.angularSharedService;}); /* now you can share the service data between two apps */ angular.module("app1", ['sharedService']) /* module code */ angular.module("app2", ['sharedService']) /* module code */我不得不承认的事实,这种解决方案并不理想 - 但这是我发现这个问题的干净的解决方案。
I have to admit the fact that this solution is not ideal - but this is the cleanest solution I found for this problem.
更多推荐
是否可以共享两个angularjs应用程序之间的数据?
发布评论