search

Home  >  Q&A  >  body text

angular.js - angular how to refresh the page

I use $state.go(); to switch pages
The data on some of these pages is dynamic and I want the data to be reloaded every time I come in.
How can I reload the page (re-execute the method in the controller) every time I switch to this page?
PS:

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

This has no effect either

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

I got the state change event here, but I don’t know how to refresh $scope...

PS2:
This is how I achieved it

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

I don’t know if this is a crude method...
But this also presents a new problem
The method in $stateChangeSuccess will be triggered twice. What is the situation?

淡淡烟草味淡淡烟草味2808 days ago615

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