繁体   English   中英

Angularjs TypeError: <serviceName> 。 <functionName> 不是Scope。$ scope的函数。 <functionName>

[英]Angularjs TypeError: <serviceName>.<functionName> is not a function at Scope.$scope.<functionName>

我定义了一个服务:

'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更新”函数未被识别为函数。

为什么我的服务无法识别为功能?

您尚未正确定义$ resource 第二个参数用于paramDefaults而不是操作

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

代替

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM