Abhishek
Abhishek

Reputation: 351

comparing $injector and injector created explicitly

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>

Here i'm trying to create an injector outside controller using angular.injector() by name explicitInjector and inside controller i'm getting $injector and on comparing both object contents are showing same. when i try to check the service present using .has() from both of there it is showing true with $injector and false with explicitInjector where it should have given true.Help me out in this,Thanks in advance

Upvotes: 0

Views: 37

Answers (1)

Plankton
Plankton

Reputation: 386

Here is a plunker:

http://plnkr.co/edit/UAVLTig4k2iZ1vLbt03B?p=preview

  • I changed the version to 1.5.8
  • used ===

The {{result}} comparison fails, which explains why the property is not found in explicitInjector.

Upvotes: 1

Related Questions