角度资源参数未通过

时间:2016-05-12 02:39:49

标签: angularjs angularjs-directive coffeescript angularjs-scope angularjs-resource

在Angular中使用资源服务进行POST时,似乎不会将参数与请求一起发送。在我添加第二个函数(limit, offset) -> (允许我动态发送请求的标头值)之前,服务运行正常

添加新分数功能

newScore = new Score()
newScore.leadership = $scope.scoreLeadership
newScore.planning = $scope.scorePlanning
newScore.attitude = $scope.scoreAttitude
newScore.accomplishment = $scope.scoreAccomplishment
newScore.total = $scope.scoreTotal
newScore.comment = $scope.scoreComment
newScore.location_id = $scope.scoreBase.id
newScore.event_id = event.id
newScore.unit_id = $scope.scoreTeam.id
newScore.save null, (data) ->
        data.unit = $scope.teams[$scope.teams.indexOf($scope.scoreTeam)]
        data.location = $scope.bases[$scope.bases.indexOf($scope.scoreBase)]
        showSuccess()
        $scope.addScore.$setPristine()
        $scope.scoreLeadership = undefined
        $scope.scorePlanning = undefined
        $scope.scoreAttitude = undefined
        $scope.scoreAccomplishment = undefined
        $scope.scoreTotal = undefined
        $scope.scoreComment = undefined
        $scope.scoreBase = undefined
        $scope.scoreTeam = undefined
        $scope.scores.push(data)

得分服务

define [
"service/services"
], (services) ->

main = ($resource, event, baseUrl) ->
    (limit, offset)->
        $resource baseUrl + "score/:id", {
            id: "@id" 
            event_id: event.id
            },
            { query: {
                method: "GET",
                headers: {'X-Response-Limit' : limit , 'X-Response-Offset' : offset},
                isArray: true
                },
            save: {
                method: "POST",
                isArray: false
                }
            }

services.factory "Score", [
    "$resource"
    "event"
    "baseUrl"
    main
]

1 个答案:

答案 0 :(得分:0)

我通过删除传递参数的空白函数,并向自己的资源添加一个函数来设置参数来解决这个问题。

limit = 15
offset = 0

res = $resource baseUrl + "person/:id/:role_id", 
    { id: "@id", role_id:"@role_id", unit_id: event.id }, 
    { query: {
        method: "GET",
        isArray: true,
        headers:
            'X-Response-Limit': () ->
                return limit
            'X-Response-Offset': () ->
                return offset
        }
    }

    res.setLimit = (_limit, _offset)->
        limit = _limit
        offset = _offset    

    res