我想调用在DeliverService中定义的服务,但是当我从控制器调用它时,它给出了Cannot read propery getRiders of undefined错误,不知道为什么会这样:|

DeliverService.js

angular.module('Deliver')
    .service('DeliverService', ['$http', '$state', '$resource', '$q', 'SettingService', '$localStorage', "MessageService", function($http, $state, $resource, $q, SettingService, $localStorage, MessageService) {
        var service = {
            getRiders : function(){
                return $http.get("Hero.json");
                //return $http.get(SettingService.baseUrl + "api/orders");
            } }
        return service;
    }]);


DeliverCtrl.js

use strict';
angular.module('Deliver').controller('DeliverCtrl',['$scope','$state', "SettingService","DeliverService", function($scope, $state, $ionicModal, MessageService, SettingService,DeliverService) {
  $scope.riders = [];
  DeliverService.getRiders().then(function(response){
    $scope.riders = response.data.data;
  }, function(error){
  });
}]);

最佳答案

您的依存关系不在此处的匹配顺序中。因此,实际上并没有注入DeliverService

您的控制器代码应如下所示:

angular.module('Deliver').controller('DeliverCtrl',
  ['$scope','$state', '$ionicModal', 'MessageService', 'SettingService','DeliverService',
  function($scope, $state, $ionicModal, MessageService, SettingService, DeliverService) {
    $scope.riders = [];
    DeliverService.getRiders().then(function(response){
      $scope.riders = response.data.data;
    }, function(error){});
}]);

10-05 20:45