Angular 服务未定义

Angular Service Undefined

在下面的代码片段中,我得到 "ReferenceError: 'ShoppingListService' is undefined"。我看不出可能是什么错误,一直在撞它并搜索了一段时间,有人有线索吗?

var ShoppingListApp = angular.module('ShoppingListApp', [])
ShoppingListApp.factory('ShoppingListService', ['$http', function ($http) {
    var ShoppingListService = {};
    ShoppingListService.getListItems = function () {
        return $http.get('/ShoppingList/GetListItems');
    };
    return ShoppingListService;
}]);

ShoppingListApp.controller('ShoppingListController', function ($scope) {
getItems();
function getItems() {
    ShoppingListService.getListItems() //Error occurs here
    .success(function (shoppingItems) {
        $scope.items = shoppingItems;
        console.log($scope.items);
    })
.[removed for brevity].

错误发生在上面指出的区域。 Angular.js 版本 1.4.9.

在您的控制器定义中 ShoppingListController 您只有一种名为 $scope 的注射器,您需要添加另一种名为 ShoppingListService.

的注射器
ShoppingListApp
  .controller('ShoppingListController', ShoppingListController);

ShoppingListController.$inject = ['$scope', 'ShoppingListService']; 
function ShoppingListController($scope, ShoppingListService) { 

    getItems();

    function getItems() {
        ShoppingListService
          .getListItems() //Error occurs here
          .success(onSuccess);  
    }

    function onSuccess(shoppingItems) {
        $scope.items = shoppingItems;
        console.log($scope.items);
    }
    //other code
}