Javascript $window对象在其他模块中不可用
我为AngularJS应用程序创建了两个单独的模块。在第一个模块中,我一登录,就将登录信息和API密钥存储在Javascript $window对象在其他模块中不可用,javascript,angularjs,Javascript,Angularjs,我为AngularJS应用程序创建了两个单独的模块。在第一个模块中,我一登录,就将登录信息和API密钥存储在$window对象中 $window.sessionStorage["apiKey"] = apiKey; $window.sessionStorage["profile"] = profile; 但是,我无法访问第二个模块中的窗口对象 我的问题: 我可以使用任何factory服务从其他模块访问窗口对象吗? 或者,如何在$window对象中保留属性?您应该为此使用服务 创作 var my
$window
对象中
$window.sessionStorage["apiKey"] = apiKey;
$window.sessionStorage["profile"] = profile;
但是,我无法访问第二个模块中的窗口对象
我的问题:我可以使用任何factory服务从其他模块访问窗口对象吗?
或者,如何在
$window
对象中保留属性?您应该为此使用服务
创作
var myService = function ($http) { // injecting $http just for the sake of it
var service = {};
service.name = "My Service";
return service;
};
添加到模块
注入控制器
谢谢@Mihai-Andrei。但是我在另一个模块中有$window。我可以在其他模块中使用它吗?是的。我已经编辑了我的答案。您可以使用这种方法将其添加到多个模块中。实际上,您可能应该为此使用常量。
app.service('myService', ['$http', myService]);
app.controller('MyController', ['$scope', 'myService', function ($scope, myService) {
$scope.serviceName = myService.name;
}]);