如何将POST之后做出AngularJS $资源更新,PUT或DELETE请求?
在我的角的应用程序,我设置项目作为 $资源
。在我的控制器,我问了数据的服务器通过GET请求时立即加载页面。我可以编辑采用了棱角分明的双向数据绑定在我的浏览器的信息,然后我可以通过一个POST请求将数据保存到服务器,就像这样:
In my Angular app, I've setup "project" as a $resource
. In my controller, I ask the server for the data over a GET request immediately when the page loads. I can edit the information in my browser using Angular's two-way data binding, and then I can save the data back to the server via a POST request, like this:
(编辑/简化为简洁起见)
(edited/simplified for brevity)
function ProjectCtrl($scope, $routeParams, Project) {
$scope.project = Project.get({id: $routeParams.projectId},
function(data) { //Successfully received data },
function(data) { //Failed to receive data },
);
$scope.saveProject = function() {
$scope.project.save();
};
}
我的节点服务器正确地接收数据,处理它,并做了一些变更,将其保存到我的数据库,然后通过JSON新更新的项目对象响应。直到我刷新页面角不显示新对象。有没有什么办法让它显示新的数据?我假设有一个更加自动化角的方式来做到这一点,而不是手动调用后 $ scope.project.save()另一个GET请求;
它看起来像服务器响应回来的第一个参数回调到保存
,从而只要服务器用的完全的对象数据的响应,你应该能够使用它来创建这样一个新项目的对象:
It looks like the server response comes back as the first argument to the callback to save
, so as long as the server responds with the full object data, you should be able to use it to create a "new" Project object like this:
function ProjectCtrl($scope, $routeParams, Project) {
$scope.project = Project.get({id: $routeParams.projectId},
function(data) { /* Successfully received data */ },
function(data) { /* Failed to receive data */ },
);
$scope.saveProject = function() {
$scope.project.save(function(data) {
$scope.project = new Project(data);
});
};
}