var myApp = angular.module('myApp', []);
var explicitInjector = angular.injector(['myApp'], true);
console.log('modchk outside ctrler', explicitInjector);
myApp.service('serv', function() {});
var x = angular.injector(['myApp'], true);
function MyCtrl1($scope, $injector) {
$scope.result = JSON.stringify($injector) == JSON.stringify(explicitInjector);
$scope.value1 = $injector.has('serv');
$scope.value2 = explicitInjector.has('serv');
}
myApp.controller('MyCtrl1', MyCtrl1);
<html ng-app='myApp'>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
<div id="div1" ng-controller="MyCtrl1">
<P>(JSON.stringify($injector)==JSON.stringify(explicitInjector))={{result}}
<hr>
<p>injector as argument to the controller($inject)={{value1}}</p>
<hr>
<p>injector from outside the controller(angular.injector())={{value2}}</p>
</div>
</body>
</html>
这里我试图使用angular.injector()
名称 explicitInjector 在控制器外部创建注入器,并且在控制器内部我得到$injector
并且在比较两个对象内容时都显示相同。
当我尝试使用.has()
来检查现有服务时,它显示true
带$ injector,false
显示带有explicitInjector,它应该给true
。帮助我在此,谢谢你提前
答案 0 :(得分:1)
这是一个吸虫:
http://plnkr.co/edit/UAVLTig4k2iZ1vLbt03B?p=preview
{{result}}
比较失败,这解释了为什么在explicitInjector中找不到该属性。