gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:16:03 24 4
gpt4 key购买 nike

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

这是代码:

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 方法返回一个常规 Promise,因此您可以使用 then 代替:

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

关于javascript - $Resource 错误 - 使用 $save.success 回调但数据保存正常时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29582091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com