Angular JS错误 - http://errors.angularjs.org/1.5.0/$injector/unpr?p0

时间:2016-02-25 18:49:29

标签: javascript angularjs

我一直收到标题中提到的错误。我曾尝试在错误上阅读Angular文档,看起来我的一切都很好。

我有3个模块App.js,planCtrl.js和gservice.js并附加如下

index.html

<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Google Libraries -->
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkISJD8ay_X92_2BJxoe1k15ICtGFf5o&libraries=places"></script>

<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<script src="https://code.angularjs.org/1.5.0/angular.min.js"></script>
<script src="https://code.angularjs.org/1.5.0/angular-route.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular-cookies.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-messages.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.1.2/ui-bootstrap-tpls.js"></script>

<script src="js/app.js"></script>
<script src="js/planCtrl.js"></script>
<script src="js/gservice.js"></script>

app.js

var chaloApp = angular
     .module('chaloApp', [
        'planCtrl', 'gservice', 'ngCookies', 
        'ngRoute', 'ngMessages', 'ui.bootstrap'
    ]);

plan.js

var planCtrl = angular.module('planCtrl', ['gservice']);

planCtrl.controller('planController', ['$scope', '$rootScope', '$http', '$cookies', '$location', '$compile', '$uibModal', 'gservice', function($scope, $rootScope, $http, $cookies, $location, $compile, $uibModal, gservice){

gservice.js

angular.module('gservice', [])
    .factory('gservice', ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http){

如果需要详细信息,则为完整错误。

Error: [$injector:unpr] http://errors.angularjs.org/1.5.0/$injector/unpr?p0=<div class="routePages ng-scope" ng-view="">copeProvider%20%3C-%20%24scope%20%3C-%20gservice
at Error (native)
at https://code.angularjs.org/1.5.0/angular.min.js:6:416
at https://code.angularjs.org/1.5.0/angular.min.js:43:7
at Object.d [as get] (https://code.angularjs.org/1.5.0/angular.min.js:40:270)
at https://code.angularjs.org/1.5.0/angular.min.js:43:69
at d (https://code.angularjs.org/1.5.0/angular.min.js:40:270)
at e (https://code.angularjs.org/1.5.0/angular.min.js:41:1)
at Object.invoke (https://code.angularjs.org/1.5.0/angular.min.js:41:86)
at Object.$get (https://code.angularjs.org/1.5.0/angular.min.js:38:460)
at Object.invoke (https://code.angularjs.org/1.5.0/angular.min.js:41:295)

一直在努力争取这一点,所以感谢任何帮助。

谢谢,

3 个答案:

答案 0 :(得分:3)

index.html

中尝试此操作
displayLayer

这是因为<script src="js/gservice.js"></script> <script src="js/planCtrl.js"></script> <script src="js/app.js"></script> 取决于planCtrlgservice取决于chaloAppplanCtrl,因此您需要先包含它们。

答案 1 :(得分:0)

您是否曾尝试首先包含gservice.js文件,因为其他模块依赖于它:

<script src="js/gservice.js"></script>
<script src="js/app.js"></script>
<script src="js/planCtrl.js"></script>

修改
您的错误消息表明$scope提供商无法注入gservice

您应该只在服务中注入$rootScope

angular.module('gservice', [])
    .factory('gservice', ['$rootScope', '$http', function($rootScope, $http)

答案 2 :(得分:0)

我建议您将gservice工厂的名称更改为与工厂模块的名称不同。具有相同的名称可能会混淆事物。如果您将gservice模块注入plan控制器模块,它应该被注入您的app模型中。

尝试以下更改:

<强> plan.js

var planCtrl = angular.module('planCtrl', []);

planCtrl.controller('planController', ['$scope', '$rootScope', '$http', '$cookies', '$location', '$compile', '$uibModal', 'newGservice', function($scope, $rootScope, $http, $cookies, $location, $compile, $uibModal, newGservice){

<强> gservice.js

angular.module('gservice', [])
    .factory('newGservice', ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http){