I have a simple question about dependency injection in Angular. I create custom services to use them in each other. Unfortunately, I get errors the way I tried to do this. This is my code:
var myApp = angular.module('app', []); myApp.service('$service1', ['$rootScope', function($rootScope) { this.test = function() { console.log('service1'); }; }]); myApp.provider('$service2', ['$service1', function($service1) { var service = 'service2'; this.registerService = function(mytext) { service = mytext; }; this.$get = function() { var that = {}; that.test = function() { console.log(service); }; return that; }; }]); myApp.config(['$service2Provider', function($service2Provider) { $service2Provider.registerService('changed service2'); }]); myApp.controller('AppCtrl', ['$rootScope', '$service1', '$service2', function($rootScope, $service1, $service2) { $service1.test(); $service2.test(); }]);
Error: [$ injector: modulerr] Unable to create the module application because of: [$ injector: unpr] Unknown provider: $ service1 http://errors.angularjs.org/1.2.0-rc.2/ $ injector / unpr ? p0 =% 24service1
If you remove the dependency $servic1 in $service2 , it will work, but why?
source share