Angularjs 工厂注入错误
Angularjs factory inject error
我正在使用 angularJS
,当我注入 Factory 时出现错误:
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
是的,
您正在做的是重新创建您的应用程序
您需要做的是定义一次并继续使用该实例
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')
这个 returns 'myapp',但添加 [],告诉 angular 创建一个应用程序而不是获取它
检查错误中的 link (https://docs.angularjs.org/error/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService):
您多次创建模块:
angular.module('myapp', [])
你应该做一次。然后在没有 []
的情况下使用
angular.module('myapp').factory ...
angular.module('myapp').controller ...
错误的原因是因为在创建 httpService
和 controller
时您使用了 setter
即 angular.module('myapp', [])
语法 module
而不是 getter
语法。 angular.module('myapp')
。
Angular要求我们只定义一次模块,后续重新定义会报错。
所以在app.js
中定义module
:
angular.module('myapp', []) ;
在 myfactory.js
中,通过删除 , []
:
使用 getter 语法
angular.module('myapp')
.factory('httpService', function($http, $timeout) {
});
并且在 test.js
中:
angular.module('myapp')
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
这里是link到docs
代码应如下所示:
angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {
});
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
我正在使用 angularJS
,当我注入 Factory 时出现错误:
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
是的,
您正在做的是重新创建您的应用程序
您需要做的是定义一次并继续使用该实例
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') 这个 returns 'myapp',但添加 [],告诉 angular 创建一个应用程序而不是获取它
检查错误中的 link (https://docs.angularjs.org/error/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService):
您多次创建模块:
angular.module('myapp', [])
你应该做一次。然后在没有 []
的情况下使用angular.module('myapp').factory ...
angular.module('myapp').controller ...
错误的原因是因为在创建 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() {
}
});
这里是link到docs
代码应如下所示:
angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {
});
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});