如何使用路径变量而不是使用AngularJS $资源的请求参数

tes*_*123 9 rest resources path-variables angularjs

可能有一种简单的方法可以做到这一点,但我似乎无法找到方法.

当我点击下面显示的删除按钮时,角度会点击以下网址:

http://localhost:8080/rest/managedCourse?id=3
Run Code Online (Sandbox Code Playgroud)

我如何让它来传递路径变量而不是像这样的请求参数:

http://localhost:8080/rest/managedCourse/3
Run Code Online (Sandbox Code Playgroud)

继承人我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

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});
    }
}
Run Code Online (Sandbox Code Playgroud)

bra*_*ing 9

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

应该这样做

  • /:id部分会被忽略吗?我只是想了解它是如何工作的. (10认同)
  • 虽然这似乎有效,但我也想知道它是如何工作的.[AngularJS关于$ resource的文档](https://docs.angularjs.org/api/ngResource/service/$resource)似乎没有解释这种行为.有关超出值的文档将作为查询参数添加,但不会跳过URL模板的部分内容 (2认同)