search

Home  >  Q&A  >  body text

angular.js - angular 如何刷新页面

我使用$state.go();来切换页面
其中有些页面的数据是动态的,我希望每次进来都重新加载数据。
请问如何每次当切换到这个页面时都重新加载页面(重新执行controller中的方法)?
PS:

$state.go('page',null,{
    reload:true
});

这个也并没有效果

app.run(['$rootScope', '$location',
    function($rootScope, $location) {
        $rootScope.$on('$stateChangeStart', function(evt, next, current) {
            alert('route begin change');
        });
    }
]);

我在这里获得了state变化的事件 然而并不知道如何刷新$scope...

PS2:
我是这样实现的

$scope.$on('$stateChangeSuccess', function(evt, next, current) {
    // 数据reload
});

不知道算不算土办法...
但是这样也出现一个新的问题
$stateChangeSuccess中的方法会触发2遍,这是什么情况呢?

淡淡烟草味淡淡烟草味2744 days ago576

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-05-15 16:54:27

    You can save data into sessionStorage. Then broadcast in the listening event of $on below. Then listen to this broadcast event in the sub-layer controller.

    reply
    0
  • 迷茫

    迷茫2017-05-15 16:54:27

    No special processing is required. The code you write in the controller will be automatically executed every time you switch to this page

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-15 16:54:27

    The controller will generate a new instance every time. Every time the state changes, the controller and the view are new instances. This is also consistent with the injection idea of ​​angularjs. You can log it yourself in the controller code.

    reply
    0
  • 迷茫

    迷茫2017-05-15 16:54:27

    "The method in $stateChangeSuccess will be triggered twice" This is because you have written Controller information in both view and Route

    reply
    0
  • Cancelreply