[英]Angularjs factory inject error
I'm using angularJS
and when I'm injecting a Factory I get error: 我正在使用angularJS
,当我注入Factory时出现错误:
app.js : app.js:
angular.module('myapp', [])
myfactory.js : myfactory.js:
angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {
});
Controller: test.js : 控制器: test.js:
angular.module('myapp', [])
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
When I add httpService
I get error. 当我添加httpService
出现错误。 Everything seems to be right, I even use this factory in all projects. 一切似乎都是正确的,我什至在所有项目中都使用了这个工厂。 Error: 错误:
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
Check the link in your error ( https://docs.angularjs.org/error/ $injector/unpr?p0=httpServiceProvider%20%3C-%20httpService): 检查错误中的链接( https://docs.angularjs.org/error/ $ injector / unpr?p0 = httpServiceProvider%20%3C-%20httpService):
You create module multiple times: 您多次创建模块:
angular.module('myapp', [])
You should do it once. 你应该做一次。 Then use without [] 然后不使用[]
angular.module('myapp').factory ...
angular.module('myapp').controller ...
The reason for the error is because in the creation of the httpService
and the controller
you have used the setter
ie angular.module('myapp', [])
syntax for the module
and not the getter
syntax. 的原因的错误是因为在创建的httpService
和controller
你已经使用了setter
即angular.module('myapp', [])
的语法的module
,而不是getter
语法。 angular.module('myapp')
. angular.module('myapp')
。 Angular requires us to define a module only once, thus the subsequent redefining causes the error. Angular要求我们仅定义一次模块,因此随后的重新定义会导致错误。
So in app.js
, define the module
: 因此,在app.js
,定义module
:
angular.module('myapp', []) ;
In myfactory.js
use the getter Syntax by removing the , []
: 在myfactory.js
,通过删除, []
使用getter语法:
angular.module('myapp')
.factory('httpService', function($http, $timeout) {
});
And in test.js
: 在test.js
:
angular.module('myapp')
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
yes, 是,
what you are doing is re-creating your app 您正在做的是重新创建您的应用
what you need to do is define it once and continue using that instance 您需要做的是定义一次并继续使用该实例
var app = angular.module('myapp', []);
app.factory('httpService', function($http, $timeout) {
});
app.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
or if you want to retrieve your app the syntax is angular.module('myapp') this returns 'myapp', but adding [], tells angular to create an app and not fetch it 或者,如果您要检索应用程序,语法为angular.module('myapp'),则返回'myapp',但是添加[],告诉angular创建一个应用程序而不获取它
Code should look like below: 代码如下所示:
angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {
});
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.