gpt4 book ai didi

javascript - 无法读取未定义的 ngResource 的属性 '$remove'

转载 作者:行者123 更新时间:2023-11-30 16:31:38 24 4
gpt4 key购买 nike

我的服务器中有一个可用的 api,我的 CRUD 模块工作正常,我可以在我的服务器中获取、发布、放置、删除

我的 Angular Controller 中也有 CRUD 模块,除了删除功能外一切正常

我正在使用 ngResource $save, $update,$remove 来处理客户端中的数据,一直到服务器

$save 和 $update 工作得很好,但是 $remove 返回一个错误:“无法读取属性 $remove of undefined”

这是我的 Controller

$scope.findOne = function () {
$scope.post = Posts.get({
postId: $routeParams.postId
});
};

$scope.update = function () {
$scope.post.$update(function () {
$location.path('posts/' + $scope.post._id);
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};

$scope.delete = function (post) {
if (post) {
post.$remove(function () {
for (var i in $scope.posts) {
if ($scope.posts[i] === post) {
$scope.posts.splice(i, 1);
}
}
});
}
else {
$scope.posts.$remove(function () {
$location.path('posts');
});
}
};

我的服务

angular.module('posts').factory('Posts', ['$resource',
function ($resource) {
return $resource('posts/:postId', {
postId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);

奇怪的是,更新有效,但删除无效

是因为我在delete方法中有参数吗?

这是我的观点

<div data-ng-controller="PostsCtrl">
<section data-ng-init="findOne()">
<div data-ng-show="authentication.user._id === post.author._id">
<a href="/#!/posts/{{post._id}}/edit">Edit</a>
<a href="#" data-ng-click="delete()">delete</a>
</div>
</section>
</div>

为什么我不能删除帖子并返回未定义?ngResource 中不再使用 $remove 了吗?

ngResource 中有哪些可用的方法?

最佳答案

像使用Posts.remove$scope.post.$remove

$scope.post.$remove(successCallBack,errorCallBack) //careful the item ($scope.post) will be removed.

Posts.remove({},{postId: 'your post id'})

$scope.delete方法的参数不包含$resource的引用

关于javascript - 无法读取未定义的 ngResource 的属性 '$remove',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251310/

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