简体   繁体   English

AngularJS-在路由更改时刷新控制器作用域

[英]Angularjs - refresh controller scope on route change

Was wondering what is the best way to refresh controller scope on route change? 想知道在路由更改上刷新控制器范围的最佳方法是什么?

Any help would be appreciated! 任何帮助,将不胜感激!

I would refactor to have all my initialisation of data properties on the scope in a single function, called something like initScope() that is called when the controller is first run, and also on the $routeChangeSuccess (and probably also $routeChangeUpdate if you want to handle changes to the URL that resolve to the same route) event(s). 我将重构有我的数据属性的所有初始化在一个单一的功能范围,称为像initScope()被调用时,控制器是第一次运行,并在$routeChangeSuccess (可能也$routeChangeUpdate如果你想处理对更改为同一路由事件的URL的更改。

eg 例如

app.controller('MyCtrl', ['$scope', function ($scope) {
    function initScope() {
        $scope.foo = 1;
        $scope.bar = { ram: 'ewe' };
    }

    initScope();

    $scope.$on('$routeChangeUpdate', initScope);
    $scope.$on('$routeChangeSuccess', initScope);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM