javascript - Ionic 应用程序中的分离服务不起作用

标签 javascript angularjs ionic-framework angular-services

我正在努力成功地将服务分离到单独的文件中。我正在尝试以与我的 Controller 相同的方式进行操作,即:

在index.html中,我有这些行

<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/services/Service1.js"></script>
<script src="js/controllers.js"></script>
<script src="js/controllers/Ctrl1.js"></script>

app.js

angular.module('testApp', ['ionic', 'testApp.controllers', 'ionic.service.core', 'testApp.services'])

Controller .js

angular.module('testApp.controllers', []);

Ctrl1.js

angular.module('testApp.controllers')
.controller('TestCtrl', function($scope) {});

但是,对服务使用相同的方法不起作用,我会收到“未知提供商”的错误。

服务.js

angular.module('testApp.services', ['ionic.utils']);

Service1.js

angular.module('testApp.services')
.factory('testService' function () {
return {}});

我也尝试过

angular.module('testApp.services', ['ionic.utils'])
.factory('testService' function () {
return {}});

我在这里缺少什么?谢谢

最佳答案

如果您在问题中提供的代码是直接从 .js 文件复制的,那么问题就是您的 Service1.js 文件中缺少逗号:

angular.module('testApp.services')
.factory('testService', function () {
//                    ^ missing comma
  return {};
});

您在 Service.js 中尝试修复的其他问题也存在同样的问题

关于javascript - Ionic 应用程序中的分离服务不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33172324/

相关文章:

javascript - 如何在 iOS 键盘上输入 "tel"时获取句点(Web/Hybrid/PWA)

angular - 如何在 ionic 2 中使用 pdfmake?

ios - Cordova 在尝试添加平台时抛出语法错误

javascript - 当请求另一个 window.open 时 window.close 当前窗口

javascript - 文本鼠标悬停时的图像边框

javascript - ionic 2 : when I pass array to component I get a string on component. ts

javascript - 是否可以将 jquery ajax 请求链接为一级队列(无嵌套)

javascript - Angular传单指令问题: other parts besides the map does't show the second time entering the page

javascript - Ionic + Angular POST 请求返回状态 404

javascript - 如何使用 ng-repeat 过滤 AngularJS 中的多个值?