关联服务和控制器AngularJS错误:未知提供者:myservicesProvider< - myservices

时间:2013-08-21 06:50:32

标签: html5 angularjs service controller

我收到此错误,我尝试了不同的方法,但仍然没有找到任何解决方案 这是我的代码:

app.js

    angular.module('myApp', [ 'myApp.services','myApp.filters', 'myApp.directives',
     'myApp.controllers']).          
     config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/Receive', {templateUrl: 'partials/partial1.html', controller: 'ReceiveCtrl'});
$routeProvider.when('/Pending', {templateUrl: 'partials/partial2.html', controller: 'PendingCtrl'});
 $routeProvider.when('/ResumePending', {templateUrl: 'partials/partial3.html', controller: 'PendingCtrl'});
$routeProvider.otherwise({redirectTo: '/view1'});
 }]);

services.js

    angular.module('myApp.services',[])
    .service('myservice',function($resource)
     {


     var pendings = $resource('myUrl2', {methode: 'GET', isArray:true});
     var items; 

var myPo='rawad al bo3bo3';
var quantity;
var barcode;


   return{
              getItems: function(){

                items =$resource('myUrl', {methode: 'GET', isArray:true});
                return items  ;  
                  },



              setQty: function(qty){
                 quantity=qty;
                 },


 deletePO : function(id){
                },


suspend:function(id){
    //URL needed
},




       }).
      value('version', '0.1');

这是我的控制者:

  angular.module('myApp.controllers', []).controller('ReceiveCtrl', ['$scope','myservice', function ($scope,myservice) {      


   $scope.po='rawad';
   alert ('WoW!! It is '+myservice.getPO);
 $scope.quantity='';
 $scope.barcode='';



   $scope.addProduct= function(){

    myservice.setQty($scope.quantity);
    myservice.setBc($scope.barcode);
    myservice.addToPliste;

      };

        }]);

在控制器中我无法访问来自我的服务的变量...因此警报消息将无效并且我收到此错误

  

错误:未知提供者:myservicesProvider< - myservices

2 个答案:

答案 0 :(得分:4)

服务声明:

angular.module('myApp.services',[]).
    service('myservice',function($resource)

控制器中的服务注入:

angular.module('myApp.controllers', []).
    controller('ReceiveCtrl', ['$scope','myservices', ...

该服务名为myservice,但在注入时使用myservices

答案 1 :(得分:1)

在您的service.js中,您已将服务命名为“服务”,而在您的控制器中,您将其称为“服务”。那是错误。将控制器中的服务更改为服务

试试这个;

在你的app.js

angular.module('myApp', [ 'myApp.services','myApp.filters', 'myApp.directives',
     'myApp.controllers', 'ngResource']).

在index.html中包含angular-resource.js文件。这可能会解决您的问题。

相关问题