角度未捕获ReferenceError:服务未定义 [英] angular Uncaught ReferenceError: Service is not defined
问题描述
我尝试注入服务的组件如下:
I have the following component in which I am trying to inject a service:
angular.
module('phoneList').
component('phoneList', {
templateUrl: '/static/common/angular/phone-list/phone-list.template.html',
controller: ['$http', 'authenticationService',
function PhoneListController($http, authenticationService) {
var self = this;
authenticationService.authenticate().then(function(){
console.log('it worked!!!!!!!!');
});
}
]
});
该服务如下所示:
angular.module('authentication').factory('authenticationService', function($http, $se){
function authenticate(){
$http.post('/o/token/', data, config)
.success(function (data, status, headers, config) {
console.log('auth service: '+data['access_token']);
$sessionStorage.access_token = data['access_token'];
});
}
function getToken(){
return $sessionStorage.access_token;
}
return {
authenticate:authenticate,
getToken:getToken
};
});
我的phone-list.module.js看起来像这样:
My phone-list.module.js looks like this:
angular.module('phonecatApp', [
'phoneList',
'authentication',
]);
angular.module('phoneList', ['authentication']);
运行此命令时出现错误:
When I run this i get the error:
未捕获的ReferenceError:未定义authenticationService
Uncaught ReferenceError: authenticationService is not defined
将'authenticationService'放在''中时,出现错误消息:
When I put 'authenticationService' in '', I get the error:
错误[$ injector:unpr] authtenticationService
Error [$injector:unpr] authtenticationService
推荐答案
服务似乎没有正确注入PhoneListController.
It seems the service isn't properly injected into the PhoneListController.
将其更改为:
controller: ['$http', 'authenticationService',
function PhoneListController($http, authenticationService) {
...
数组中的字符串只是为了保持注入的依赖项引用的安全.仍然需要将该服务添加为函数参数.
The strings in the array are just to keep the injected dependency references minification safe. The service still needs to be added as a function argument.
还请确保为每个组件调用angular.module
一次:
Also be sure to call angular.module
once for each component:
app.module.js
app.module.js
angular.module('phonecatApp', [
'ngRoute',
'phoneList',
'authentication',
]);
phone-list.module.js
phone-list.module.js
angular.module('phoneList', ['authentication']);
这篇关于角度未捕获ReferenceError:服务未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!