angularJS - 我的服务没有被称为获取错误

时间:2017-05-25 14:21:08

标签: angularjs angular-services

我的应用配置:

(function( ){
    "use strict";

    angular.module("blog", ["ngRoute",  "blog.controllers", "blog.services"  ]);

    function config ($locationProvider, $routeProvider) {

        $locationProvider.html5Mode(true);

        $routeProvider
        .when('/', {
            templateUrl: 'modules/users/views/users-list.tpl.html',
            controller: 'User.controller',
            controllerAs: 'userlist'
        });
    }

    angular
    .module('blog')
    .config(config);

})( );

我的服务:

(function( ){

    "use strict";
    angular.module("blog.services", ["ngResource"])
    .service("userService", function( ) {

        console.log("service called");

        this.name = function( ) {
            // return $resource(BaseUrl + ‘/posts/:postId’, { postId: ‘@_id’ });
            return "Tech Mahindra";
        }

    })


})( );

我的控制员:

(function( ){
    "use strict";

    angular.module("blog.controllers", [ "userService" ])
    .controller("User.controller", function(  service ) {

        var userlist = this;
        userlist.name = "Nissan New Japan!!";
        console.log( "user controller ready!!");

        console.log("from service post : " , service.name( ) ); //throws error


        return userlist;
    });
})( );

这里有什么问题?任何人帮助我?

我得到的错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module blog due to:
Error: [$injector:modulerr] Failed to instantiate module blog.controllers due to:
Error: [$injector:modulerr] Failed to instantiate module userService due to:
Error: [$injector:nomod] Module 'userService' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

error link

1 个答案:

答案 0 :(得分:2)

您应该将blog.services模块注入blog.controllers应用而不是userService

angular.module("blog.controllers", [ "blog.services" ])
.controller("User.controller", function(userService ) {