Angularjs TypeError:<servicename>。<functionname>不是Scope的函数。$ scope。<functionname>

时间:2017-04-10 18:56:54

标签: javascript angularjs angular-services

我定义了一项服务:

'use strict';

angular.module('vAppName.services')
   .factory('UpdateWarehouse', function($resource, configSettings) {
     var updateWarehouse = $resource(configSettings.apiServiceUrl + 'api/v1/updateWarehouse', {
       'update': {
         method: 'POST'
       }
     });
     return updateWarehouse;
   }); 

在控制器中,列出控制器中的服务:

angular.module('vAppName.controllers')
    .controller('VCtrlName', [<list of other properties>'UpdateWarehouse',
        function(<list of other properties>, updateWarehouse) { 
        ...bunch of functions and properties

            $scope.updateWrh = function() {
                updateWarehouse.update({

                    startDate: $scope.updateWrhParams.startDate,
                    endDate: $scope.updateWrhParams.endDate

                }).$promise.then(function(){
                     $scope.messageModalVariables = {
            messageTitle: 'Warehouse Update Complete',
            messageDisplay: 'Warehouse has been updated.',
            messageType: 'Success',
            okIsHidden: false,
            yesNoIsHidden: true
          };
                    $scope.openMessageModal($scope.messageModalVariables);

                }, function (error) {
                     $scope.messageModalVariables = {
            messageTitle: 'Error Warehouse Update',
            messageDisplay: error.data,
            messageType: 'Error',
            okIsHidden: false,
            yesNoIsHidden: true
          };
          $scope.openMessageModal($scope.messageModalVariables);
                });
            };

        } //end main function
    ]);

这是HTML

<div style="margin: 5%; display: inline-block;">
                                <label id="startDateLabel" style="margin-left: 5%">Start Date:</label>
                                <date-picker id="startDatePicker" type="text" model="updateWrhParams.startDate" style="width: 50%; float: right;"></date-picker>
                            </div>
                            <div style="margin: 5%; display: inline-block;">
                                <label id="endDateLabel" style="margin-left: 5%">End Date:</label>
                                <date-picker id="endDatePicker" type="text" model="updateWrhParams.endDate" style="width: 50%; float: right;"></date-picker>
                            </div>
<button type="button" id="updateBtnWrh" class="btn btn-success" style="margin-left: 3%; background-color: #41a334;" ng-disabled="isAdmin == true" ng-click="updateWrh()">Update</button>

单击Update' button, the function updateWrh()is called but the更新时,服务中的功能无法识别为功能。

为什么我的服务未被识别为功能?

1 个答案:

答案 0 :(得分:0)

您尚未正确定义$resource。第二个参数是 paramDefaults 而非操作

 var updateWarehouse = $resource(URL, {}, {
   'update': {
     method: 'POST'
   }
 });

而不是

 var updateWarehouse = $resource(URL, {
   'update': {
     method: 'POST'
   }
 });