AngularJS拥有自己的工厂和ngResource

时间:2014-06-19 03:43:08

标签: javascript angularjs

我已经创建了自己的LoggerService但是如果我使用它我会收到错误:

FOrder.query is not a function

如果我删除控制器中的所有LoggerService调用都工作正常,为什么我不能使用LoggerService? services.js

angular.module('GSAdmin.services', ['ngResource'])
    .factory('FOrder', ['$resource', function($resource) {
        return $resource('/api/order/:orderId');
    }])
   .service('LoggerService', [function(){
        var _logList = [];
        this.getLast = function(){
            return _logList[_logList.length-1];
        };
        this.getLog = function(){
            return _logList;
        };
        this.log = function(text) {
           _logList.push(text);
        };
}])    

controller.js

    .controller('OrderController', ['$scope', 'FOrder', 'LoggerService',
        function($scope, FOrder, LoggerService) {
            FOrder.query(function(data){
                 $scope.orders = data;
        });

        $scope.log = LoggerService.getLog();
        LoggerService.log('Begin editing order #' + field.id);

    }]);

2 个答案:

答案 0 :(得分:0)

.controller('OrderController', ['$scope', 'FOrder', 'LoggerService',      function($scope, FOrder, LoggerService) {
            FOrder.query(function(data){
                 $scope.orders = data;
        });

        $scope.log = LoggerService.getLog();
        LoggerService.log('Begin editing order #' + field.id);
} <-- **you missed that bracket**
    ]);

答案 1 :(得分:0)

问题出在&#39; FOrder&#39; LoggerService&#39;,我不知道为什么重要,但当我将其切换到“LoggerService&#39;”时,&#39; FOrder&#39;,一切都很好。