远程更改数据后刷新范围 [英] Refresh of scope after remote change to data

查看:28
本文介绍了远程更改数据后刷新范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的基于 Angular1 的 mpbile 应用程序的控制器中有(例如)以下功能:

var getItem = function() {//初始化 $scope$scope.url = "(获取我的数据的url)";$http.get($scope.url).success(function(data) {$scope.itemDetails = 数据;//从json获取数据});};获取项目();

这工作得很好..有一个问题..它不会更新.即使我切换页面并返回,如果范围没有改变,也不会反映范围内的新数据.

所以,我内置了一个 $interval refresh 来查找范围的变化,这很好用,除了当我离开页面转到另一个页面时,该间隔会继续轮询.在数据和电池使用可能会成为问题的移动应用中,这显然是个坏主意.

那么.. 我怎样才能在仅在该页面上检查实时更改"的范围,或者什么是范围刷新数据更改的最佳实践.

我已经阅读了关于摘要和应用的内容,但这些似乎仍然是间隔检查,我怀疑在切换页面后会继续运行.

或者在具有实时数据的 Angular 应用程序上,不断轮询 API要做的事情"(诚然,页面拉取的数据只有 629 字节,但我有几页可以保存实时数据,所以它会添加上)

谢谢

解决方案

根据你使用的路由器,你必须告诉控制器在路由改变或更新时重新加载,因为你在声明控制器时传递的函数是只有一个工厂,一旦控制器被构建,它就不会再次运行,因为路由器缓存它(除非你告诉 angularjs 这样做,这很少是一个好主意).

所以最好的办法是在路由改变时使用路由器重新加载状态.您可以使用范围内广播的路由器事件更改和更新来完成此操作.

如果您使用 angularjs 的路由器(又名,ngRoute):

$scope.$on('$routeChangeUpdate', getItem);$scope.$on('$routeChangeSuccess', getItem);

如果您使用 ui.router:

$scope.$on('$stateChangeUpdate', getItem);$scope.$on('$stateChangeSuccess', getItem);

<块引用>

注意:ui.router 中,您可以在状态声明中添加 cache: false ,它会阻止控制器和视图被缓存.

In my controller for a mpbile app based on Angular1 is have (for example) the following function:

var getItem = function() {

    // Initialize $scope
    $scope.url = "(url to get my data)";

    $http.get($scope.url).success(function(data) {
        $scope.itemDetails = data; // get data from json
    });
};    

getItem();

and this works just fine.. with one problem.. it doesnt update. Even if I switch pages and come back, if the scope hasnt changed, it doesnt reflect new data in the scope.

So, i built in an $interval refresh to look for changes in the scope, this works fine EXCEPT, when i leave the page to go to another, that interval keeps polling. This is obviously a bad idea in a mobile app where data and battery usage may be an issue.

So.. how can I keep checking the scope for 'live changes' when ON that page only OR what is best practice for the scope to refresh on data changes.

I have read about digests and apply but these still seem to be interval checks which I suspect will keep operation after switching pages.

Or on angular apps with live data, is constantly polling the API the 'thing to do' (admittedly the data the page pulls is only 629 bytes, but i have a few pages to keep live data on, so it will add up)

Thanks

解决方案

Depending on the router you are using, you have to tell the controller to reload when the route changed or updated, because the function you pass when declaring a controller is only a factory, and once the controller is constructed it won't run again because the router caches it (unless you tell angularjs to do so, which is rarely a good idea).

So your best bet is to use the router to reload the state when the route changes. You can do this using the router event change and update that is broadcast in the scope.

If you are using angularjs' router (a.k.a., ngRoute):

$scope.$on('$routeChangeUpdate', getItem);
$scope.$on('$routeChangeSuccess', getItem);

If you are using ui.router:

$scope.$on('$stateChangeUpdate', getItem);
$scope.$on('$stateChangeSuccess', getItem);

Note: in ui.router you can add cache: false on the state declaration and it'll prevent the controller and the view to be cached.

这篇关于远程更改数据后刷新范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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