且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

远程更改数据后刷新范围

更新时间:2023-12-03 14:33:28

根据所使用的路由器,您必须告诉控制器在更改或更新路由时重新加载,因为在声明控制器时传递的功能是只是一个工厂,一旦构建了控制器,它就不会再次运行,因为路由器会对其进行缓存(除非您告诉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.