AngularJS 共享服务不作为模块之间的单例
AngularJS shared service not acting as singleton between modules
这个问题我见过很多,但似乎找不到适合我的问题的正确答案。
以下问题是服务无法在其他服务之间共享数据 modules/controllers。它不充当单例。意思是,如果我在 dashboard
中添加 windows 并调用 windowService.getWindows()
,我将看到我添加的 windows。但是,如果我在通过仪表板添加它们后从 myWidget
执行相同的操作,反之亦然,则服务中的集合为空,就好像另一个 module/controller 没有添加任何内容一样。我在这里做错了什么?我希望该服务保留 windows 的单个集合,并且可以从我的其他模块中访问。
模块 1
var dashboard = angular.module('dashboard', [windowServiceModule]);
dashboard.controller('DashboardController', function DashboardController(windowService) {...stuff});
模块 2
var myWidget = angular.module('myWidget', [windowServiceModule]);
myWidget.controller('MyWidgetController', function MyWidgetController(windowService) {...stuff});
服务
var windowServiceModule = angular.module('windowService', []);
windowServiceModule.service('windowService', function() {
var windows = [];
this.addWindow = function(win)
{
this.windows.push(win)
}
this.getWindows = function()
{
return this.windows;
}
});
编辑:在浏览其他答案时,我试过这个:Share a single service between multiple angular.js apps
我认为这暴露了一部分问题。上面建议迭代 $rootScope
集合,但在我的例子中,集合中只有一个 $rootScope
,并且 dashboard
和 myWidget
之间不相同。实际上,myWidget
在 iframe 中,现在我想我需要采取不同的方法,目前正在测试类似于我在评论中留下的 link 的解决方案。
好吧,如果 myWidget
没有加载到 iframe 中,我很确定我的初始代码可以正常工作。我有一个可以打开 windows 嵌入或弹出的仪表板,我想使用该服务来管理 windows 并处理来回传递的状态,因为弹出 iframe 需要重新加载页面。
无论如何,我现在在服务中有这样的东西并且它正在工作:
var windowServiceModule = angular.module('windowService', []);
windowServiceModule.service('windowService', function($window) {
...stuff
var parentScope = $window.parent.getScope();
...other stuff
之后,您可以使用 parentScope 访问父级范围内的任何内容。我传入 angular $window
.
仅供参考,getScope()
已存在于父 JSP 文件中。这是它的样子:
function getScope()
{
return angular.element($("#DashboardController")).scope();
}
这个问题我见过很多,但似乎找不到适合我的问题的正确答案。
以下问题是服务无法在其他服务之间共享数据 modules/controllers。它不充当单例。意思是,如果我在 dashboard
中添加 windows 并调用 windowService.getWindows()
,我将看到我添加的 windows。但是,如果我在通过仪表板添加它们后从 myWidget
执行相同的操作,反之亦然,则服务中的集合为空,就好像另一个 module/controller 没有添加任何内容一样。我在这里做错了什么?我希望该服务保留 windows 的单个集合,并且可以从我的其他模块中访问。
模块 1
var dashboard = angular.module('dashboard', [windowServiceModule]);
dashboard.controller('DashboardController', function DashboardController(windowService) {...stuff});
模块 2
var myWidget = angular.module('myWidget', [windowServiceModule]);
myWidget.controller('MyWidgetController', function MyWidgetController(windowService) {...stuff});
服务
var windowServiceModule = angular.module('windowService', []);
windowServiceModule.service('windowService', function() {
var windows = [];
this.addWindow = function(win)
{
this.windows.push(win)
}
this.getWindows = function()
{
return this.windows;
}
});
编辑:在浏览其他答案时,我试过这个:Share a single service between multiple angular.js apps
我认为这暴露了一部分问题。上面建议迭代 $rootScope
集合,但在我的例子中,集合中只有一个 $rootScope
,并且 dashboard
和 myWidget
之间不相同。实际上,myWidget
在 iframe 中,现在我想我需要采取不同的方法,目前正在测试类似于我在评论中留下的 link 的解决方案。
好吧,如果 myWidget
没有加载到 iframe 中,我很确定我的初始代码可以正常工作。我有一个可以打开 windows 嵌入或弹出的仪表板,我想使用该服务来管理 windows 并处理来回传递的状态,因为弹出 iframe 需要重新加载页面。
无论如何,我现在在服务中有这样的东西并且它正在工作:
var windowServiceModule = angular.module('windowService', []);
windowServiceModule.service('windowService', function($window) {
...stuff
var parentScope = $window.parent.getScope();
...other stuff
之后,您可以使用 parentScope 访问父级范围内的任何内容。我传入 angular $window
.
仅供参考,getScope()
已存在于父 JSP 文件中。这是它的样子:
function getScope()
{
return angular.element($("#DashboardController")).scope();
}