AngularJS Error: Unknown provider: sharedServiceProvider <
I'm follow this example http://jsfiddle.net/simpulton/XqDxG/, also find here http://youtu.be/1OALSkJGsRw
var myModule = angular.module('myModule', []);
myModule.factory('mySharedService', function($rootScope) {
var sharedService = {};
sharedService.message = '';
sharedService.prepForBroadcast = function(msg) {
this.message = msg;
this.broadcastItem();
};
sharedService.broadcastItem = function() {
$rootScope.$broadcast('handleBroadcast');
};
return sharedService;
});
After always getting this message
Error: Unknown provider: sharedServiceProvider <- sharedService
I just copy exactly the code of jsfiddle. My surprise. The same error. Crazy, cause at jsfiddle the code work perfectly.
Could be this code deprecated?
Since the jsFiddle runs fine and problems only start when you move the code into your application I presume that the issue is in the application initialization logic.
You should make sure that your ng-app
directives specifies a module name you want to use, for example: ng-app="myModule"
. If you check the "Info" panel in the jsFiddle you can see that it has the body tag configured like so: <body ng-app="myModule">
.
In any case your issue is not linked to any version changes / framework features deprecated, we can easily update AngularJS to the latest version and it runs just fine. Here is the updated jsFiddle: http://jsfiddle.net/fRzAD/1/
I had the same error, the solution for me was put all parameters in inject.
function addPedidoController ($scope, $http, $location, $rootScope, sharedService) {
..
}
addPedidoController.$inject = ['$scope', '$http', '$location', '$rootScope', 'mySharedService'];
I hope it's useful for you!
链接地址: http://www.djcxy.com/p/77628.html上一篇: AngularJS:错误:未知提供者