如何刷新本地数据使用$资源服务于AngularJS一次读取 [英] How to refresh local data fetched using $resource service in AngularJS

查看:147
本文介绍了如何刷新本地数据使用$资源服务于AngularJS一次读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用$资源服务使用query()方法来检索数据,并使用新的模型数据AngularJS应用。$ save()方法。这工作正常完全一样的文档说应该。

我的问题是如何更新使用MyService.query()首先我的本地数据提取我已经改变了它?

我采取了最简单的方法,现在,我只需再次调用query()方法。我知道这是最糟糕的方式效率明智的,但它是最简单的一种。

在我的服务器端我回到了新模式的全州,再presentation。我怎样才能在新创建的模型添加到本地数据的阵列?

更新结果
我已经结束了简单地把来自服务器的型号回报,但我还是很高兴地知道,如果这是要走的路。从我可以从源头上code返回数组明白的是计划之久的JavaScript阵列,我可以操纵自己。

这是code我用

  $ scope.save =功能(){
    VAR newComment =新CommentsDataSource();
    newComment.Content = $ scope.todoText;
    。newComment $保存({ID:1},功能(savedComment){
        $ scope.comments.push(savedComment);
    });
}


解决方案

我只想再次获得整个清单,可以看到其他用户带到列表的修改。

但是,如果解决方案,您正在使用适合你,然后使用它。这是corrrect。角采用最基本的JavaScript对象。在报表中添加一个新的实例列表将刷新页面上显示的列表。

I have AngularJS application that use $resource service to retrieve data using query() method and create new data using model.$save() method. This works fine exactly as the docs say it should.

My question is how to update my local data fetched using MyService.query() in the first place after I've changed it?

I took the most simple approach for now and I simply call the query() method again. I know this is the worst way efficiency-wise but it's the simplest one.

In my server-side I return the whole state-representation of the new model. How can I add the newly created model to the array of the local data?

UPDATE
I've end up simply pushing the model return from the server but I'll still be happy to know if that's the way to go. From what I can understand from the source code the return array is plan-old-javascript-array that I can manipulate myself.

This is the code I used

$scope.save = function () {
    var newComment = new CommentsDataSource();
    newComment.Content = $scope.todoText;
    newComment.$save({ id: "1" }, function (savedComment) {
        $scope.comments.push(savedComment);
    });
}

解决方案

I would simply get the whole list again, to be able to see the modifications brought to the list by other users.

But if the solution you're using suits you, then use it. It's corrrect. Angular uses bare-bones JavaScript objects. Adding a new instance to a list in the scope will refresh the list displayed on the page.

这篇关于如何刷新本地数据使用$资源服务于AngularJS一次读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆