9

There is probably an easy way to do this but I can't seem to find out how.

When I click on my delete button shown below angular hits the following url:

http://localhost:8080/rest/managedCourse?id=3

How can I get it to hit pass a path variable instead of a request parameter like this:

http://localhost:8080/rest/managedCourse/3

Heres my html:

<table>
    <tr ng-repeat="course in page.content">
        <td>{{course.title}}</td>
        <td>{{course.description}}</td>
        <td>{{course.creditValue}}</td>
        <td><button ng-click="remove(course.id)">Delete</button></td>
    </tr>
</table>

And here is my controller:

function ManagedCourseController($scope, $resource) 
{
    var ManagedCourse = $resource("rest/managedCourse/:courseId", {courseId:'@id'});

    $scope.page = ManagedCourse.getPage({"page.page": "0", "page.size": "3", "page.sort": "title", "page.sort.dir": "asc"});


    $scope.create = function (managedCourse) {
        ManagedCourse.create(managedCourse);
    }

    $scope.remove = function (courseId) {
        ManagedCourse.remove({id:courseId});
    }
}

1 Answer 1

9
function ManagedCourseController($scope, $resource) 
{
    var ManagedCourse = $resource("rest/managedCourse/:courseId/:id", 
       {courseId:'@id'});
    ...

should do it

Sign up to request clarification or add additional context in comments.

5 Comments

Will the /:id part just get ignored then? I just want to understand how that works.
Though this seems to work, I'm also wondering how this works. The AngularJS documentation about $resource seems not to explain this behavior. It documents about excess values are added as query parameters but not how parts of the URL template are skipped
And how would you do it for a custom action ?
I find the example above confusing. One could assume the @id is related to the second path parameter.
I successfully make use of a $resource with a path parameter without using the {courseId:'@id'} at all. And I wonder why the doc and the examples I see around all boast this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.