使用Angular 1.5和ES6类,我有一个操作应用程序。
我创建了一项服务:
class EnterpriseService {
/*@ngInject*/
constructor($http) {
this.http = $http;
};
getData() {
return this.http.get('http://localhost:3000/').then(function(response) {
return response;
});
}
}
EnterpriseService.$inject = ["$http"];
export default EnterpriseService;
我把它注入我的控制器:
class EnterpriseController {
/*@ngInject*/
constructor(EnterpriseService, $scope) {
this.name = 'enterprise';
this.systemId = 20003
this.pageLink = '#/enterprise';
this.EnterpriseService = EnterpriseService;
this.getEnterpriseData();
this.scope = $scope;
console.log(this.EnterpriseService);
}
getEnterpriseData() {
scope.stores = this.EnterpriseService.getData();
}
}
EnterpriseController.$inject = ["EnterpriseService", "$scope"];
export default EnterpriseController;
我将服务导入到引导整个组件的组件文件中:
import template from './enterprise.html';
import controller from './enterprise.controller';
import './enterprise.less';
import servicesModule from './enterprise.service';
let enterpriseComponent = function () {
return {
restrict: 'E',
scope: {},
template,
controller,
controllerAs: 'vm',
bindToController: true
};
};
export default enterpriseComponent;
截至目前,我收到臭名昭着的未知服务提供商错误。我的假设是,它与我通过将其包含在Angular模块上而不是将我的服务类实例化为Angular服务这一事实有关,即:angular.module('enterpriseService', enterpriseService)
但我不确定如何在这个ES6环境中这样做。
是否应该有另一个文件引导所有服务模块然后被包含?那究竟是什么样的?
答案 0 :(得分:1)
看起来你正试图调用使用" $ scope"的函数。在保存范围之前。尝试移动this.scope = $ scope;高于this.getEnterpriseData():在你的代码中:
class EnterpriseController {
/*@ngInject*/
constructor(enterpriseService, $scope) {
this.name = 'enterprise';
this.systemId = 20003
this.pageLink = '#/enterprise';
this.EnterpriseService = EnterpriseService;
this.getEnterpriseData();
this.scope = $scope;
console.log(this.EnterpriseService);
}
它应该是这样的:
class EnterpriseController {
/*@ngInject*/
constructor(enterpriseService, $scope) {
this.name = 'enterprise';
this.systemId = 20003
this.pageLink = '#/enterprise';
this.EnterpriseService = EnterpriseService;
this.scope = $scope;
this.getEnterpriseData();
console.log(this.EnterpriseService);
}