自定义 angular 服务注入失败
Custom angular service injection failing
我在将自定义服务注入控制器时遇到问题。当我尝试在页面上使用控制器时,收到以下错误:
Error: [$injector:unpr] http://errors.angularjs.org/1.3.14/$injector/unpr?p0=<div id="organizations-section" ng-include="'admin/organizations.html'" class="ng-scope">copeProvider%20%3C-%20%24scope%20%3C-%orgService
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:6:417
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:38:7
at Object.d [as get] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:13)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:38:81
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:13)
at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:283)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:432)
at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:37:184)
at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:315)
这是我的 JavaScript 文件,它定义了应用程序、服务和控制器:
admin/admin.js
adminApp = angular.module("admin", []);
adminApp.service("orgService", ["$scope", "$http", function($scope, $http){
}]);
adminApp.controller("OrganizationController", ["$scope", "orgService", function($scope, orgService){
}]);
admin.html.erb
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body ng-app="admin">
<div id="organizations-section" ng-include="'admin/organizations.html'"></div>
</body>
</html>
organizations.html
<div ng-controller="OrganizationController as orgCtrl">
<h2>Organizations</h2>
</div>
我应该注意到这是一个 Rails 4.2 应用程序。我试图删除尽可能多的代码来简化它,但我仍然看到问题。如果我从 Controller 定义中删除 orgService
,没问题。但我似乎无法让那个工作。我查看了其他服务定义示例,但没有发现问题。
在你的控制器中使用依赖注入时,你声明 $scope
和 orgService
作为依赖,但是你的函数有三个参数,包括 notifyService
.
我认为您还需要在依赖项中声明 notifyService
:
adminApp.controller('OrganizationController', ['$scope', 'orgService', 'notifyService',
function($scope, orgService, notifyService){
}]);
确保服务 notifyService
存在或将其从 OrganizationController
的依赖项中删除。
您的服务声明有问题
You can't inject $scope into services, there is no such thing as a
Singleton $scope.
如果需要,可以添加注入 $rootScope
。但通常 service
用于在其中放置常用方法,就像您可以从中进行 ajax 调用,然后 return 适当的数据或承诺调用方方法。
代码
adminApp = angular.module("admin", []);
adminApp.service("orgService", ["$http",
function($http){
}
]);
adminApp.controller("OrganizationController", ["$scope", "orgService",
function($scope, orgService){
}
]);
希望对您有所帮助谢谢。
我在将自定义服务注入控制器时遇到问题。当我尝试在页面上使用控制器时,收到以下错误:
Error: [$injector:unpr] http://errors.angularjs.org/1.3.14/$injector/unpr?p0=<div id="organizations-section" ng-include="'admin/organizations.html'" class="ng-scope">copeProvider%20%3C-%20%24scope%20%3C-%orgService
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:6:417
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:38:7
at Object.d [as get] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:13)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:38:81
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:13)
at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:283)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:432)
at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:37:184)
at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:315)
这是我的 JavaScript 文件,它定义了应用程序、服务和控制器:
admin/admin.js
adminApp = angular.module("admin", []);
adminApp.service("orgService", ["$scope", "$http", function($scope, $http){
}]);
adminApp.controller("OrganizationController", ["$scope", "orgService", function($scope, orgService){
}]);
admin.html.erb
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body ng-app="admin">
<div id="organizations-section" ng-include="'admin/organizations.html'"></div>
</body>
</html>
organizations.html
<div ng-controller="OrganizationController as orgCtrl">
<h2>Organizations</h2>
</div>
我应该注意到这是一个 Rails 4.2 应用程序。我试图删除尽可能多的代码来简化它,但我仍然看到问题。如果我从 Controller 定义中删除 orgService
,没问题。但我似乎无法让那个工作。我查看了其他服务定义示例,但没有发现问题。
在你的控制器中使用依赖注入时,你声明 $scope
和 orgService
作为依赖,但是你的函数有三个参数,包括 notifyService
.
我认为您还需要在依赖项中声明 notifyService
:
adminApp.controller('OrganizationController', ['$scope', 'orgService', 'notifyService',
function($scope, orgService, notifyService){
}]);
确保服务 notifyService
存在或将其从 OrganizationController
的依赖项中删除。
您的服务声明有问题
You can't inject $scope into services, there is no such thing as a Singleton $scope.
如果需要,可以添加注入 $rootScope
。但通常 service
用于在其中放置常用方法,就像您可以从中进行 ajax 调用,然后 return 适当的数据或承诺调用方方法。
代码
adminApp = angular.module("admin", []);
adminApp.service("orgService", ["$http",
function($http){
}
]);
adminApp.controller("OrganizationController", ["$scope", "orgService",
function($scope, orgService){
}
]);
希望对您有所帮助谢谢。