2

资源工厂:

.factory('WorkerRepository', function($resource){
    return $resource('workers/:id', {id:'@id'});
})

控制器:

.controller('ListController', function($scope, WorkerRepository){
    var workers = WorkerRepository.query(function(){
        $scope.workers = workers;
    });

    $scope.worker = {nameSurname: 'Peter', email: "test@gmail.com", phone: 600100200};

    $scope.add = function() {
        var worker = new WorkerRepository(this.worker);
        worker.$save();
    };
})

执行方法时$scope.add,脚本会引发TypeError: Object #<g> has no method 'push'错误。如果我理解正确$resource$save方法默认只提供'save': {method:'POST'},所以没有isArray: true。那么为什么我会收到这个错误?

4

1 回答 1

2

显然,错误存在于我的后端 REST 控制器中(感谢@Stewie 的提示!) - 我没有将 POST 方法链接到操作,因此$save()方法调用了负责的操作query(),它确实返回了一个对象数组。

曾是:

query() -> GET  /workers -> return array of workers
save()  -> POST /workers -> return array of workers

后端控制器重构后:

query() -> GET  /workers -> return array of workers
save()  -> POST /workers -> return created worker
于 2013-07-15T10:19:08.377 回答