0

我有一个关于离子服务的问题。如您所知,Ionic 框架带有一个内置的 www 目录,其中包含 js 目录,其中包含 services.js 文件。我的第一个服务有效。我试图在同一个 services.js 文件中编写另一个服务,结果出现错误:Uncaught "SyntaxError: Unexpected token . http://localhost:8100/js/services.js行:57" 除了“(索引):28 未捕获的错误:[$injector:modulerr] 无法实例化模块启动器,原因是:错误:[$injector:modulerr] 无法实例化模块 starter.services,原因是:错误: [$injector:nomod] 模块“starter.services”不可用!您要么拼错了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。”无论如何,这是我的代码,请看一下,让我知道我可以做些什么不同的事情。另外,如果您需要更多片段,请告诉我!

//services.js 
//the first factory, API works 
angular.module('starter.services', [])
.factory('API', function($http) {
    var apiFooUrl = 'http://localhost:3000/api/do/thing'
    return {
        all: function(){
              return $http.get(apiFooUrl + 's')
        },

        show: function(thingID){
              console.log("stock service get running", thingID);
              return $http.get(apiFooUrl + '/' + thingID)
        }
  };
});
//this is the one that returns the error
.factory('Users', function($http) {
    var apiUrl = 'http://localhost:3000/api/users/'

    return {

        index: function(){
          return $http.get(apiUrl)
        }

        show: function(id){
          return $http.get(apiUrl + id)
   }
  }
})

编辑以获取更多信息:因此响应 David L 的评论:我已将其注入到我的 GlobalCtrl 中,如下所示:

.controller('GlobalCtrl', function(Users, $rootScope, $state, $window,   
Things, $scope){
      $scope.newUser = {}
      $scope.user = {}

      //show all Users
      Users.index().success(function(results){
       $scope.users = results
      })
})

它也将被注入到 DetailCtrl 中。在 app.js 中,服务是这样注入的:

angular.module('starter', ['ionic', 'starter.controllers', 
'starter.services'])

最终还有很多事情要做,所以我想确保我现在做对了。

我是否正确包含了 starter.controllers?如果我有多个,我是否必须包括多个?它们确实包含在 index.html 文件中。

4

1 回答 1

0

关闭模块后添加了服务,去掉分号就可以解决这个问题

}) //here reomved the semicolon
//this is the one that returns the error
.factory('Users', function($http) {
于 2016-06-04T04:45:01.447 回答