Javascript 角度1.5.4服务未定义
我正在使用AngularJS 1.5.4开发一个演示应用程序,它基于angular seed、EcmaScript 6和node.js web服务器 我使用的是这里描述的组件路由器:当从组件调用服务时,我得到一个错误 错误是Javascript 角度1.5.4服务未定义,javascript,angularjs,ecmascript-6,Javascript,Angularjs,Ecmascript 6,我正在使用AngularJS 1.5.4开发一个演示应用程序,它基于angular seed、EcmaScript 6和node.js web服务器 我使用的是这里描述的组件路由器:当从组件调用服务时,我得到一个错误 错误是 etsy.controller.es6:17 Uncaught ReferenceError: EtsyService is not defined 以下是相关代码 index.html <script src="/bower_components/angular/
etsy.controller.es6:17 Uncaught ReferenceError: EtsyService is not defined
以下是相关代码
index.html
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/bower-angular-router/angular1/angular_1_router.js"></script>
<script src="/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>
<script src="/app.js"></script>
<script src="/components/core/constants.es6"></script>
<script src="/components/etsy/etsy.service.es6"></script>
<script src="/components/etsy/etsy.controller.es6"></script>
etsy.controller.es6
(function () {
'use strict';
class EtsyController {
constructor($scope) {
$scope.message = 'Hi from the $scope';
}
}
angular
.module('myApp.etsy', [])
.service('etsyService', EtsyService)
.component('etsy', {
templateUrl: 'components/etsy/etsy.html',
controller: EtsyController
});
})();
我已经在谷歌上搜索了一段时间,但我不明白我做错了什么。有什么想法吗
谢谢您需要在iLife中的etsy.controller.es6文件中为etsysservice定义一个函数。您可以查看该指南如何实现heros.js和crisis.js,以获得一个具体示例 尝试将名称文件更改为etsysservice.js,这应该会起作用。如果您按该顺序调用(服务和控制器),那么您将通过创建一个新模块来覆盖该模块:
angular
.module('myApp.etsy')// <--------- getting a module
.factory('EtsyService', EtsyService.etsyServiceFactory);
angular
.module('myApp.etsy', [])//<--------------- creating a new module
.service('etsyService', EtsyService)
angular
.module('myApp.etsy')//谢谢Eric,这肯定是一个解决方案,但是有没有办法不用函数引用服务?这似乎真的是多余的,否则你可以使用依赖注入来传递你的服务,从控制器中删除.module('myApp.etsy',[])
,基本上,这是重新初始化您的模块我尝试了@PankajParkar,但是我得到了相同的错误。我可以知道为什么您在etsy.controller.es6
中再次创建etsysservice
,因为它已经在etsy.service.es6
中创建了,为什么这与此相关?根据样式指南,建议的名称是feature.type.js(ref:),我按照您的建议进行了更改,但仍然得到相同的错误:(
angular
.module('myApp.etsy')// <--------- getting a module
.factory('EtsyService', EtsyService.etsyServiceFactory);
angular
.module('myApp.etsy', [])//<--------------- creating a new module
.service('etsyService', EtsyService)