AngularJs angular.injector、angular.module

angular.injector

创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入。

格式:angular.injector(modules);

modules:  Array 注入的模块(一个或多个)。

使用代码:

复制代码
  (function () {
    angular.module("firstModule", [])
    .service("firstService", function () {
      this._log = function () {
          console.log("Hello World!!!");
      }
  });
    angular.module("Demo", [])
    .controller("testCtrl", testCtrl);
    function testCtrl() {
      var injector = angular.injector(["firstModule"]);
      injector.get("firstService")._log();//Hello World!!!
    };
  }());



在上面的代码里,我们没有在angular.module里对Demo模块进行firstModule模块的依赖注入,那么是不是就意味我们不能在Dome模块里使用firstModule模块的函数了咯?其实并不然,Angular 有$injector服务来处理注入这件事情。我们这里的angular.injector也是依赖这个服务的,所以我们能在Dome模块里使用它引入firstModule模块,然后调用firstModule模块里的方法。

这个方法是创建一个对象,这个对象大家可以打印出来看看,对象上有一些方法,具体关于他的描述将会在学习及翻译到$injector的时候详细的讲解。我们这里用到的是get方法,使用这个方法获取到firstModule模块的service服务,然后再执行里面的_log函数。

angular.module

创建一个全局的可用于检索和注入的Angular模块。所有Angular模块(Angular核心模块或者第三方模块)想要在应用里实现,都需要使用这个注入机制。

格式:angular.module(name,[requires],[configFn]);

name :  string  创建的模块名称。

[requires]: 字符串的数组  代表该模块依赖的其他模块列表,如果不依赖其他模块,则为空数组。

[configFn]:对该模块的一些配置。

使用代码:

  angular.module("Demo", ["ui.router"],function(){//config })
也可以这么写

写在一般的配置文件里面 var gVarMyAppStarterModule = angular.module("starter",[]); //应用主模块
//应用所要依赖的模块 写在app.js当中
gVarMyAppStarterModule.requires = [
  'ionic',
  'ngCordova',
  'ngAnimate',
  'ngCookies',
  "myApp.directive",
  'myApp.factory',
  'myApp.Service',
  'myAppIonicAngular',
];



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值