在看一个$injector用法的demo,下面是demo的所有代码:
<!DOCTYPE html> <html lang="en" ng-app="a4_6"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="../bower_components/angular/angular.min.js"></script> </head> <body> <h1>has和get方法的示例</h1> <p ng-controller="c4_6"> <!--视图组件--> </p> </body> <script type="text/javascript"> var a4_6 = angular.module('a4_6',[]) .factory('$custom', function(){ return { print: function(msg){ console.log(msg); } }; }); var injector = angular.injector(['a4_6','ng']); var has = injector.has('$custom'); console.log(has); if(has){ var custom = injector.get('$custom'); custom.print("控制台输出任意的内容"); } a4_6.controller('c4_6',['$scope','$custom', function($scope,$custom){ //控制器代码 }]); </script> </html>
var injector = angular.injector(['a4_6','ng']);
这句话里的ng是什么意思啊?
过去多啦不再A梦2017-05-15 17:07:17
Refers to the core ‘ng’ module, that is, the angular core module var injector = angular.injector(["ng"]) creates its own $injector instead of the $injector automatically created when the application starts. To create $injector yourself, you need to pass a module list. In the above example, there are two modules 'a4_6' and 'ng'. If you need to use any service in Angular core you must explicitly specify ng module. The angular.module method will assume that you have a dependency on the ng module and will quietly add "ng" to your dependency list, while the injector function will not make any assumptions about the dependency module.