2016-09-13 9 views
0

こんにちは私は角度が新しく、簡単なプログラムを作成しようとしていますが、エラーが発生します。angularjsインジェクタのエラーが発生する

コントローラー:

angular.module('myApp', []).controller('myCtrl', function ($scope ,HandleService) { 

    $scope.AddCar = function() { 

     HandleService.GetAll().then(function (result) { 
      alert(result); 
     }); 
    } 

}); 

サービス

angular.module('MyService', []).factory('HandleService', function() { 

    return { 

     GetAll: function(){ 


      return "bbb"; 
     } 
    }; 

}); 

index.htmlを

<script src="libs/angular/angular.js"></script> 
<script src='app.js'></script> 

<div ng-app="myApp" ng-controller="myCtrl"> 


    <button ng-click="AddCar()" >Save</button> 




</div> 

私は角度でこれにしようとしていますし、私はエラーを取得:

を」 angular.js:9778エラー:[$ angular.jsでHandleService http://errors.angularjs.org/1.2.16/ $インジェクター/ UNPR P0 = HandleServiceProvider%20%3C-%20HandleService - HandleServiceProvider <:78 angular.jsで :3705 Object.getServiceで[GETとしてインジェクタ:UNPR]不明なプロバイダ? (angular.js:3832)angular.jsで :呼び出しで :3710のgetServiceで (3832 angular.js)(angular.js:3859)Object.instantiateで (angular.js:3880) 角度で.js:7134 (angular.js):6538 for forach(angular.js:330) "

答えて

0

エラーは3つの異なるモジュールを作成しているためです。 1つのモジュールを別のモジュールに挿入するか、そのままモジュールに挿入します。

  1. myAppモジュールにサービスモジュールを注入します。

    angular.module('myApp', ['MyService「]

  2. あるいは、

    angular.module('myApp', []).controller('myCtrl', function ($scope ,HandleService) { 
    
        $scope.AddCar = function() { 
    
         HandleService.GetAll().then(function (result) { 
          alert(result); 
         }); 
        } 
    
    }); 
    
    
    angular.module("myApp").factory('HandleService', function() { 
    
        return { 
    
         GetAll: function(){ 
    
    
          return "bbb"; 
         } 
        }; 
    
    }); 
    
0

サービスは別の世界にあります。 myAppモジュールからMyServiceモジュールへの依存関係を宣言する必要があります。

angular.module('myApp', ['MyService']).controller('myCtrl', function ($scope, HandleService) { 
    $scope.AddCar = function() { 
     var result = HandleService.GetAll(); 
     console.log(result); 
    } 
}); 


angular.module('MyService', []).factory('HandleService', function() { 
    return { 
     GetAll: function() { 
      return "bbb"; 
     } 
    }; 
}); 
関連する問題