ngResource不传递参数 - 如何调试?

时间:2016-01-03 15:44:33

标签: javascript angularjs rest angular-resource

我有以下资源定义:

myservices.factory('User', ['$resource',
    function($resource){
        return $resource('/user/:screenName', {}, {
            whoami: {
                url: '/user',
                method: 'GET',
                isArray: false,
                params: {
                    op: 'whoami'
                }
            },
            query: {
                url: '/user',
                method: 'GET',
                isArray: true
            },
            get: {
                method: 'GET',
                isArray: false
            },
            update: {
                method:'GET',
                params: {
                    op: 'update'
                },
                isArray: false,
            },
            delete: {
                method:'GET',
                params: {
                    op: 'delete'
                },
                isArray: false,
            }

        });
    }]);

我认为它将传递screenName作为网址的一部分。不幸的是,这不会发生。

控制器代码如下:

            var user = new User();
            user.firstname = $scope.selectedUser.firstName;
            user.lastname = $scope.selectedUser.lastName;
            user.screenName = $scope.selectedUser.screenName;
            user.password1 = $scope.selectedUser.password1;
            user.password2 = $scope.selectedUser.password2;
            user.roles = $scope.selectedUser.roles;
            user.maximalCalories = $scope.selectedUser.maximalCalories;

            user.$update();

实际上它通过了:

GET http://localhost:8080/user?op=update HTTP/1.1
Host: localhost:8080
...
Accept: application/json, text/plain, */*
...
Referer: http://localhost:8080/app/index.html

即。除了显式的参数之外,它既不传递参数。

UDPATE

如果我这样做

    User.$update(
                {
                    firstname : $scope.selectedUser.firstName,
                    lastname : $scope.selectedUser.lastName,
                    screenName : $scope.selectedUser.screenName,
                    password1 : $scope.selectedUser.password1,
                    password2 : $scope.selectedUser.password2,
                    roles : $scope.selectedUser.roles,
                    maximalCalories : $scope.selectedUser.maximalCalories,
                }
            );

我收到例外TypeError: User.$update is not a function

更新2

显然,Angular将函数$update添加到对象并将函数update添加到类中,如果我在LHS处有对象,它将仅通过POST传递...

2 个答案:

答案 0 :(得分:1)

您没有将.accordion-container h3{ color: #ee1c31; font-weight: 600; padding-bottom: 5px; } 传递给资源。应该是:

screenName

答案 1 :(得分:0)

问题是您的$resource udpate对象的请求类型错误,它应该有方法PUT而不是GET

update: {
    method: 'PUT', //<--change it 'PUT' from 'GET'
    params: {
        op: 'update'
    },
    isArray: false,
},

你也有错误的删除方法类型,它应该有method: 'DELETE'而不是method: 'GET'