简体   繁体   English

angularjs出现注射器错误

[英]angularjs get an error of injector

hello i am new in angular and i am trying to build a simple program but i get an error. 你好,我是angular的新手,我正在尝试构建一个简单的程序,但出现错误。

Controller: 控制器:

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

    $scope.AddCar = function () {

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

});

Service 服务

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

    return {

        GetAll: function(){


            return "bbb";
        }
    };

});

index.html 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>

i am trying to this in angular and i get an error of: 我试图在角度,这我得到一个错误:

"angular.js:9778 Error: [$injector:unpr] Unknown provider: HandleServiceProvider <- HandleService http://errors.angularjs.org/1.2.16/ $injector/unpr?p0=HandleServiceProvider%20%3C-%20HandleService at angular.js:78 at angular.js:3705 at Object.getService [as get] (angular.js:3832) at angular.js:3710 at getService (angular.js:3832) at invoke (angular.js:3859) at Object.instantiate (angular.js:3880) at angular.js:7134 at angular.js:6538 at forEach (angular.js:330)" “ angular.js:9778错误:[$ injector:unpr]未知提供程序:HandleServiceProvider <-HandleService http://errors.angularjs.org/1.2.16/ $ injector / unpr?p0 = HandleServiceProvider%20%3C-%20HandleService在object.getService处angular.js:3705处angular.js:78 [as get](angular.js:3832)在getService(angular.js:3832)处angular.js:3710 ),位于forEach(angular.js:330)的angular.js:6538的angular.js:7134的Object.instantiate(angular.js:3880)”

Your service lives in a separate universe. 您的服务生活在一个单独的世界中。 You need to declare dependency on MyService module from your myApp module. 您需要从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";
        }
    };
});

The error is because you are creating 3 different module. 该错误是因为您正在创建3个不同的模块。 Either Inject one module to another module or just go with on module. 要么将一个模块注入另一个模块,要么直接与模块一起使用。

  1. Inject service module to myApp module. 将服务模块注入myApp模块。

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

  2. Or, 要么,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM