2017-04-20 59 views
0

我想調用我在DeliverService中定義的服務,但是當我從控制器調用它時,出現Cannot read propery getRiders of undefined錯誤,不知道爲什麼會發生這種情況:無法讀取未定義的propery getRiders

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){ 
    }); 
}]); 

回答

2

你的依賴是不是在這裏的優先順序。因此,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){}); 
}]); 
+0

是:'D It now now –

+0

Thankyou so much you save m y天 –

+0

@mariasalahuddin很高興幫助! :) 一旦定時器關閉,你可以接受答案,以便它可以幫助其他人 – tanmay

1

在DeliverCtrl.js

注射參數和功能參數不匹配

它應該是這樣的

['$scope','$state','$ionicModal','MessageService','SettingService','DeliverService', function($scope, $state, $ionicModal, MessageService, SettingService,DeliverService)