赞
踩
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', ];
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。