如何正确调用我的工厂服务?

时间:2015-05-21 10:49:12

标签: javascript angularjs ngresource

我发现'不是一个功能'尝试调用我的服务时出错。 NgResource已经注入。我做错了什么?

controller.js:

app.controller('controllerA', ['$scope', 'CustomizingService', 
    function($scope, CustomizingService) {
        $scope.cust = CustomizingService.Customizing.squares({numberOf: 2, valid: true});
}]);

service.js:

(function () {

    angular.module('ST_CCG').factory('CustomizingService', ['$resource', function ($resource) {
        //var url = config.CustomizingAPI_Url + '/Customizing/:link/';
        var url = 'http://localhost:54483/BusinessService.svc/:link/';

        return {
            Customizing: $resource(url,  {
                squares: {
                    method: 'GET',
                    params: { link: 'GetSquares', numberOf: '@numberOf', valid: @valid },
                    isArray: true },
                 circles: {
                    method: 'GET',
                    params: { link: 'GetCircles',  valid: true },
                    isArray: true }
            })
        };
    }]);
})();

修改

我正在使用chrome调试器并出现以下错误: TypeError:CustomizingService.Customizing.orders不是函数

app.js:

var app = angular.module('ST_CCG', [
  'ngRoute',
  'ngResource'
])
.config(['$routeProvider', 
        function ($routeProvider) {
            $routeProvider.
                when('/menu', {
                    templateUrl: 'Modules/Menu/view.html',
                    controller: 'controllerA'
                }).
                when('/orders', {
                    templateUrl: 'Modules/Orders/view.html',
                    controller: 'controllerB'
                }).
                otherwise({
                    redirectTo: '/'
                });
        }
]);

的index.html:

...
<!--MODOLE + CONTROLLER Resources-->
<script src="app.js"></script>
<script src="service.js"></script>
<script src="modules/menu/controller.js"></script>
<script src="modules/orders/controller.js"></script>
</head>

<body ng-app="ST_CCG">

<a href="#/menu">Menu</a>
<a href="#/orders">Orders</a>

<div ng-view></div>

</body>

1 个答案:

答案 0 :(得分:2)

这可能是因为您在默认参数的位置传递操作。将其更改为

Customizing: $resource(url, {valid: true}, {
    squares: // etc

我使用valid: true作为默认参数,因为您似乎在两个操作中都使用它。