$Resource 错误 - 使用 $save.success 回调但数据保存正常

$Resource error - when using $save.success callback but data is saving OK

我正在尝试使用资源来保存一些数据,但发生了一些奇怪的事情:

代码如下:

var appointment = new Appointment(formData);
                    console.log(appointment);
                    appointment.$save().success(function (data, status, headers, config){
                         console.log(data);
                         $state.transitionTo('form.success');
                });

现在奇怪的是,数据正在保存,所以 $save 函数可以正常工作,但我收到 undefined is not a function 错误,并且成功回调不工作 - 有人知道为什么吗?

约会的资源是:

angular.module('MyApp')
.factory('Appointment', function($resource){
    return $resource('/api/admin/:id', { id: "@_id" }, {
         query: {method:'GET', isArray:true}
    });

})

我正在使用它及其固有的 $save 函数,所以这应该不是问题 - 而且它似乎是回调不起作用 - 保存正在添加数据没问题

解决方案:

我通过以下方式简化了它:

appointment.$save(function(data){
//stuff here for the callback works
});

我也觉得用promise方法.then可以,不过没试过。

无论如何我都将其标记为已接受,以示善意!

谢谢

success 函数特定于 $http 服务:

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: success and error.

$save 方法 returns 是一个常规的 promise,因此您可以使用 then 代替:

appointment.$save().then(function (data, status, headers, config){
                     console.log(data);
                     $state.transitionTo('form.success');
            });