我正在使用angular 1.4.4(基于模块的架构)。以下是我的文件结构。我试图从服务类调用rest API(用spring-boot编写)。但是,我在TypeError中收到错误:无法读取未定义的属性'get'。
这是我的控制器类
class ConfigurationController {
/*@ngInject*/
constructor($rootScope, ConfigurationService){
Object.assign(this, {$rootScope, ConfigurationService});
this.name = 'configuration';
let vm = this;
vm.scheduleTimePeriod = 12;
vm.dataTTL=30;
}
loadData() {
this.ConfigurationService.getAllAccessPoint().then((response) => {
console.log(response);
}, (err) => {
console.log(err);} );
}
}
export default ConfigurationController;
这是我的服务类
class ConfigurationService{
/*@ngInject*/
constructor($rootScope, $http, Rest){
Object.assign(this, ($rootScope, $http, Rest));
}
getAllAccessPoint()
{
// return this.Rest.one('/accessPoint/list').getList();
this.$http.get("/configuration")
.then(function successCallback(response){
$rootScope.configuration = response.data;
console.log('load configuration data: ' + $rootScope.configuration);
}, function errorCallback(response){
console.log('Unable to perform get request');
});
return $rootScope.configuration;
}
}
export default ConfigurationService
错误听起来像是服务没有正确获取HTTP对象,可能缺少其依赖性。但是当我直接在控制器中使用它时,事情正常。我错过了什么?
答案 0 :(得分:1)
更改ConfigurationService
Object.assign(this,($ rootScope,$ http,Rest));
到
Object.assign(this, {$rootScope, $http, Rest});
答案 1 :(得分:0)
let configurationModule = angular.module('configuration', [
'ui.router',
'ui.bootstrap',
'agGrid',
'kendo.directives'
])
.config(/*@ngInject*/($stateProvider)=>{
$stateProvider
.state('configuration', {
url: '/configuration',
template: '<configuration></configuration>',
ncyBreadcrumb: {
label: 'Configuration'
}
});
})
.service('ConfigurationService', configurationService)
.directive('configuration', configurationComponent);