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

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

问题描述

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

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.

因此,我建立了一个$ interval刷新以查找范围的变化,除了我离开该页面转到另一个页面时,这个间隔很好,但是这个间隔一直在轮询.在移动应用程序中,这显然不是一个好主意,在移动应用程序中,数据和电池的使用可能是一个问题.

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.

或者在具有实时数据的角度应用程序上,不断轮询API的事情"(诚然,页面提取的数据仅为629字节,但是我有几个页面可以保留实时数据,因此它将添加上)

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)

谢谢

推荐答案

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

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.

如果您使用的是 angularjs的路由器(又称ngRoute):

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

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

如果您使用的是 ui.router :

If you are using ui.router:

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

注意:在ui.router中,您可以在状态声明上添加cache: false,这样可以防止对控制器和视图进行缓存.

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天全站免登陆