gpt4 book ai didi

javascript - Angular $更新资源

转载 作者:行者123 更新时间:2023-11-30 17:14:10 24 4
gpt4 key购买 nike

编辑:如果在我不发布更多代码的情况下很难进行故障排除,请告诉我,我可以在问题中添加更多内容。


我有两个代码基本相同的实例,其中一个可以工作,而另一个不能。

工作正常:

$scope.update = function() {
var question = $scope.question;
question.$update(function() {
$location.path('questions/' + question._id);
});
};

不工作:

$scope.updateAnswer = function() {
var answer = $scope.answerEdited;
answer.$update(function() {
$location.path('questions/' + $routeParams.questionId);
});

$scope.toggleEditAnswer(answer);
};

我在 $update 函数中收到错误:undefined is not a function。这是我的资源:

angular.module('intquestApp')
.factory('Answers', function ($resource) {
return $resource('api/answers/:answerId', {
answerId: '@_id'
}, {
update: {
method: 'PUT'
}
});
});

我究竟做错了什么?如果我正确理解流程,answer.$update 会以某种方式使用资源吗?另外,如果我使用 console.log 查看正在更新的 answer,那是正确的:

Resource {_id: "543d8896039390eb5e000001", created: "2014-10-14T20:33:26.514Z", creator: Object, questionid: "543d8693994fc1685d000001", __v: 0…}

我该如何解决这个问题?

最佳答案

您可以尝试以下操作并检查它是否有效吗?

$scope.updateAnswer = function() {
Answers.update($scope.answerEdited, function(response) {
$location.path('questions/' + $routeParams.questionId);
});

$scope.toggleEditAnswer(answer);
};

关于javascript - Angular $更新资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26432957/

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