简体   繁体   中英

Angular JS Error: [$injector:unpr] Unknown provider

I am new to angular js, I am trying to make a custom service, i have copy and paste a piece of code making a custome service but i am always getting this error,

Error: [$injector:unpr] Unknown provider: serviceProvider <- service <-         MainController
http://errors.angularjs.org/1.4.3/$injector/unpr?p0=serviceProvider%20%3C-  %20service%20%3C-%20MainController
at REGEX_STRING_REGEXP (angular.js:68)
at angular.js:4262
at Object.getService [as get] (angular.js:4409)
at angular.js:4267
at getService (angular.js:4409)
at Object.invoke (angular.js:4441)
at $get.extend.instance (angular.js:8999)
at nodeLinkFn (angular.js:8109)
at compositeLinkFn (angular.js:7541)
at publicLinkFn (angular.js:7416)(anonymous function) @ angular.js:12330

here is my custom service implementation

(function () {
    var myModule = angular.module('app', ['onsen']);
    myModule.service('service', ["$http", function ($http) {
        var test = function (usertoken) {
            alert("fffffffffffffffffffff");

        }
        return {
            getUserSites: getUserSites
        };
    }]);
})();

and here is my module implementation :

(function () {
    var myApp = angular.module('app', ['onsen']);

    var MainController = function ($scope, $http, $interval, service) {
        $scope.clciclme = function () {
            alert("HIIIIIIIIIIIIIIIIIIII");
        }
    }
    myApp.controller('MainController', MainController);
})();

Please help, what is making this error ?

You are using the module setter syntax twice, that is:

var myApp = angular.module('app', ['onsen']);

You are creating the app module twice. To use the getter syntax, simply don't pass the second parameter (the array):

var myApp = angular.module('app');

Your code would look like this:

Controller:

(function () {
    var myApp = angular.module('app', ['onsen']); // use the setter syntax one time

    var MainController = function ($scope, $http, $interval, service) {
        $scope.clciclme = function () {
            alert("HIIIIIIIIIIIIIIIIIIII");
        }
    }
    myApp.controller('MainController', MainController);
})();

Service:

(function () {
    var myModule = angular.module('app'); // use the getter syntax all subsequent times

    myModule.service('service', ["$http", function ($http) {
        var test = function (usertoken) {
            alert("fffffffffffffffffffff");

        }
        return {
            getUserSites: getUserSites
        };
    }]);
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM