gpt4 book ai didi

javascript - $window 对象在其他模块中不可用

转载 作者:行者123 更新时间:2023-11-30 17:08:54 24 4
gpt4 key购买 nike

我为 AngularJS 应用创建了两个独立的模块。在我登录后的第一个模块中,我将登录信息和 API key 存储在 $window 对象中。

$window.sessionStorage["apiKey"] = apiKey;
$window.sessionStorage["profile"] = profile;

但是,我无法访问第二个模块中的窗口对象。

我的问题:
我可以使用任何工厂服务从其他模块访问窗口对象吗?
或者,如何保留 $window 对象中的属性?

最佳答案

您应该为此目的使用服务

创作

var myService = function ($http) { // injecting $http just for the sake of it
var service = {};
service.name = "My Service";

return service;
};

添加到模块

app.service('myService', ['$http', myService]);

注入(inject) Controller

app.controller('MyController', ['$scope', 'myService', function ($scope, myService) {
$scope.serviceName = myService.name;
}]);

关于javascript - $window 对象在其他模块中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27395249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com