【发布时间】:2016-09-11 21:32:30
【问题描述】:
我尝试在以下组件中注入服务:
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 看起来像这样:
angular.module('phonecatApp', [
'phoneList',
'authentication',
]);
angular.module('phoneList', ['authentication']);
当我运行它时,我得到了错误:
未捕获的 ReferenceError: authenticationService 未定义
当我将 'authenticationService' 放入 '' 时,出现错误:
错误 [$injector:unpr] authtenticationService
【问题讨论】:
-
@A1rPun 我试过了,我得到了同样的错误。
-
您定义了两次
angular.module('phonecatApp'),这意味着您想创建两次模块,这是不可能的。您的错误authtenticationService中也有错误(可能是错字?)。
标签: javascript jquery angularjs angular-services