我正在使用angularJS
,当我注射工厂时,我收到错误:
app.js:
angular.module('myapp', [])
myfactory.js:
angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {
});
控制器: test.js:
angular.module('myapp', [])
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
当我添加httpService
时,我收到了错误消息。一切似乎都是对的,我甚至在所有项目中都使用这个工厂。的错误:
angular.min.js:92 Error: [$injector:unpr] http://errors.angularjs.org/1.2.25/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService
at Error (native)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:6:450
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:202
at Object.c [as get] (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:270
at c (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at d (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:6)
at Object.instantiate (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:165)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:67:419
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:54:25
答案 0 :(得分:6)
检查错误中的链接(https://docs.angularjs.org/error/ $ injector / unpr?p0 = httpServiceProvider%20%3C-%20httpService):
您多次创建模块:
angular.module('myapp', [])
你应该做一次。然后在没有[]
的情况下使用angular.module('myapp').factory ...
angular.module('myapp').controller ...
答案 1 :(得分:3)
出错的原因是因为在创建httpService
和controller
时,您使用了setter
angular.module('myapp', [])
语法module
而不是getter
语法。 angular.module('myapp')
。
Angular要求我们只定义一次模块,因此随后的重新定义会导致错误。
所以在app.js
中,定义module
:
angular.module('myapp', []) ;
在myfactory.js
中删除, []
:
angular.module('myapp')
.factory('httpService', function($http, $timeout) {
});
在test.js
:
angular.module('myapp')
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
以下是docs
的链接答案 2 :(得分:1)
您正在做的是重新创建应用
您需要做的是定义一次并继续使用该实例
var app = angular.module('myapp', []);
app.factory('httpService', function($http, $timeout) {
});
app.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
或者如果您想要检索您的应用,语法是angular.module(' myapp') 这会返回' myapp',但添加[]会告诉angular创建一个应用而不是获取它
答案 3 :(得分:0)
代码应如下所示:
angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {
});
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});