2017-04-10 49 views
0

我所定义的服务:

'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更新'功能中的服务不被识别为功能。

为什么我的服务不被识别为函数?

回答

0

您尚未正确定义$resource。第二个参数是paramDefaults行动

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

,而不是

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

哎呀......原来如此!我使用另一个应用程序复制资源的格式。它没有参数,也没有空括号,但用这种方式定义了函数:'.factory(UpgradeDB',function($ rootScope,$ resource,CONFIG){var upgradeDB = $ resoruce(CONFIG.apiServiceUrl +'api/upgradedb ',{'save':{method:'POST'}}); return updateDB;});'是否添加参数$ rootScope不需要使用空括号作为参数? –

+0

@GloriaSantin,$ rootScope没有任何区别,为什么角色版本正在被其他应用程序使用? – Satpal

+0

在我添加资源的应用程序中使用了v1.4.12,而具有用作模板的资源'UpdateDB'的版本使用了v1.2.23 –