角度,如何正确映射REST对象



我在Angular上非常新手,并且通过我创建的服务绘制其余的呼叫的对象有点麻烦。

我有服务

var teacherServices = angular.module('teacherServices', ['ngResource']);
teacherServices.factory('Items', ['$resource',
function($resource) {
    return $resource('/class/item/:class_id', {Id: "@Id"}, {
        query: {method:'GET', params:{class_id:''}, isArray:false}
    });
}]);

我正在尝试使用

获取对象的列表
teacher_app.controller('ItemsCtrl', ['$scope', '$route', 'Items', function($scope, $route, Items) {
Items.get({class_id : $route.current.params.classId}, function(response) {
    $scope.items = response.items;
    $scope.items[0].$save();  //Does not exist
});

}]);

我有两个问题

1)我将如何正确映射对象,以便它具有所有默认功能,例如$ save()

2)如何为对象创建自定义功能

谢谢

谢谢钱德曼尼,你让我朝着正确的方向思考!

问题是我使用的是标准GET方法,而不是我在服务中定义的"查询"方法。我还将响应调用更改为成为数组而不是持有数组的对象。

teacherServices.factory('Items', ['$resource',
function($resource) {
    return $resource('/class/item/:class_id', {Id: "@Id"}, {
        query: {method:'GET', params:{class_id:''}, isArray:true}
    });
}]);

teacher_app.controller('ItemsCtrl', ['$scope', '$route', 'Items', function($scope, $route, Items) {
Items.query({class_id : $route.current.params.classId}, function(response) {
    $scope.items = response;
    console.log($scope.items);
});

}]);

最新更新