2017-05-25 26 views
0

我的应用程序配置:angularJS - 我的服务不叫得到错误

(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

+0

你可以张贴错误? –

回答

2

你应该注入blog.services模块为blog.controllers应用程序,而不是userService

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

那么怎么调用这个名字的方法呢? – 3gwebtrain

+0

我得到如下错误:'错误:[$ injector:unpr]未知的提供者:serviceProvider < - service < - User.controller' – 3gwebtrain

+1

尝试在您的控制器中注入名称'userService'而不是'service',就像现在一样。 –