ES6 angular 服务模块没有被正确传递?
ES6 angular service module not being passed around correctly?
使用 Angular 1.5 和 ES6 classes,我有一个运行应用程序。
我创建了一个服务:
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;
截至目前,我收到了臭名昭著的未知服务提供商错误。我的假设是,它与我从未通过将服务 class 包含在 Angular 模块中将其实例化为 Angular 服务这一事实有关,即:angular.module('enterpriseService', enterpriseService)
但我不确定如何在这个 ES6 环境中执行此操作。
是否应该有另一个文件来引导所有服务模块然后被包含?那到底会是什么样子?
您似乎在尝试在保存范围之前调用使用“$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);
}
使用 Angular 1.5 和 ES6 classes,我有一个运行应用程序。
我创建了一个服务:
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;
截至目前,我收到了臭名昭著的未知服务提供商错误。我的假设是,它与我从未通过将服务 class 包含在 Angular 模块中将其实例化为 Angular 服务这一事实有关,即:angular.module('enterpriseService', enterpriseService)
但我不确定如何在这个 ES6 环境中执行此操作。
是否应该有另一个文件来引导所有服务模块然后被包含?那到底会是什么样子?
您似乎在尝试在保存范围之前调用使用“$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);
}