gpt4 book ai didi

AngularJS从不同模块访问服务

转载 作者:行者123 更新时间:2023-12-03 01:18:44 24 4
gpt4 key购买 nike

'use strict';

var trkiApp = angular.module('trkiApp', [
'trkiApp.tStatus',
'trkiApp.feed'
]);



var tStatus = angular.module('trkiApp.tStatus', [])
.factory('Status', ['$q']);

var feed = angular.module('trkiApp.feed', []);

现在我不明白我如何可以访问在另一个模块上定义的服务状态?

'use strict';

feed
.controller('FeedController', ['$scope','$http','Status']);

我应该不会吧?但不知怎的,我是……或者这是正确的行为吗?

最佳答案

一个Module是在引导过程中应用于应用程序的配置和运行 block 的集合。模块可以将其他模块列为其依赖项。依赖于模块意味着需要在加载需要模块之前加载必需模块。

var myModule = angular.module('myModule', ['module1','module2']);

当您注入(inject)模块时,服务会在配置阶段注册,您可以访问它们,因此长话短说,这是 Angular 中依赖注入(inject)的正确行为和核心基础。例如

angular.module('module1').service('appservice', function(appservice) {
var serviceCall = $http.post('api/getUser()',"Role");
});

那么如何使用 angular.module('myModule'); 访问它

angular.module('myModule').controller('appservice', function(appservice)
{
var Servicedata= appservice.ServiceCall('role');
}

这是如何访问它的。如果有人有其他建议,请说出来。

关于AngularJS从不同模块访问服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966858/

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