简体   繁体   中英

Angularjs RESTul Resource Request

I am trying to make the request

....port/trimService/fragments/?fragment_name=:fragmentName

However if I try to make the "?fragment_name" a parameter, it breaks. As I am going to have more requests, my action with change so I cannot leave it in the url portion of the resource.

angular.module(foo).factory('FragmentService', ['$resource',
function ($resource)
{
    var FragmentService = $resource('.../fragments/:action:fragmentName',
    {},
    {
        'getFragments':
        {
            method: 'GET',
            isArray: true,
            params:
            {
                fragmentName: "@fragmentName",
                action: "?fragment_name="
            }
        }

    });
    return FragmentService;
}
]);

As of right now, I have no idea what my URL is actually outputting.

EDIT: I changed my resource as /u/akonsu had mentioned below. I also added my controller as it is still not working correctly.

angular.module(foo).factory('FragmentService', ['$resource',
function ($resource)
{
    var FragmentService = $resource('.../fragments/',
    {},
    {
        'getFragments':
        {
            method: 'GET',
            isArray: true,
            params:
            {
                fragmentName: "@fragmentName",
            }
        }

    });
    return FragmentService;
}
]);


angular.module(foo).controller('FragmentController', ['$scope', 'FragmentService',
function ($scope, FragmentService)
{
    $scope.fragmentQuery = {
        fragmentName: 'a',
    };


    $scope.fragmentQuery.execute = function ()
    {
        if ($scope.fragmentQuery.fragmentName == '')
        {
            $scope.fragments = {};
        }
        else
        {
            $scope.fragments = FragmentService.getFragments(
            {
                fragmentName: $scope.fragmentQuery.fragmentName,
            });
        }
    };
    $scope.fragmentQuery.execute();

}

]);

Try omitting the query string altogether in the resource URL and just supply your fragmentName as a parameter to the action call. It should add it to the query string if it is not in the list of URL parameters.

$resource(".../port/trimService/fragments/").get({fragmentName: 'blah'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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