来自不同模块的 AngularJS 访问服务 [英] AngularJS access service from different module

查看:25
本文介绍了来自不同模块的 AngularJS 访问服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'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', []);

现在我不明白我怎么可能访问在另一个模块上定义的服务状态?

And now i dont understand how is possible that i can access the service Status which is defined on another module?

'use strict';

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

我应该不会吧?但不知何故,我是......或者这是一种正确的行为?

I should not right? But somehow i am...or is that a correct behaviour?

推荐答案

A Module 是在引导过程中应用于应用程序的配置和运行块的集合.模块可以列出其他模块作为它们的依赖项.依赖于模块意味着需要在加载需要模块之前加载必需模块.

A Module is a collection of configuration and run blocks which get applied to the application during the bootstrap process. Modules can list other modules as their dependencies. Depending on a module implies that required module needs to be loaded before the requiring module is loaded.

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

当你注入你的模块时,服务在配置阶段注册并且你可以访问它们,所以长话短说,这是正确的行为,也是 Angular 依赖注入的核心基础.例如

When you injected your module, the services got registered during the configuration phase and you could access them, so to make a long story short, it's the correct behavior and the core fundamentals of dependency injection in Angular. For example

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');
}

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

This how it can be accessed. If anyone has another suggestion please say so.

这篇关于来自不同模块的 AngularJS 访问服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆