Angular JS错误:[$ injector:unpr]未知提供者

时间:2015-08-03 14:31:29

标签: javascript angularjs ionic-framework ionic onsen-ui

我是角度js的新手,我正在尝试制作自定义服务,我复制并粘贴了一段代码来制作客户服务,但我总是收到此错误,

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

这是我的自定义服务实现

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

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

这是我的模块实现:

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

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

请帮忙,是什么造成这个错误?

1 个答案:

答案 0 :(得分:6)

您正在使用模块 setter 语法两次,即:

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

您正在创建app模块两次。要使用 getter 语法,只需传递第二个参数(数组):

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

您的代码如下所示:

控制器:

(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);
})();

服务:

(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
        };
    }]);
})();