AngularJs: Reload page

前端 未结 13 2146
孤城傲影
孤城傲影 2020-11-28 19:28
PORTAL_NAME

I want to reload the page. How can I do this?

相关标签:
13条回答
  • 2020-11-28 20:05

    window object is made available through $window service for easier testing and mocking, you can go with something like:

    $scope.reloadPage = function(){$window.location.reload();}
    

    And :

    <a ng-click="reloadPage"  class="navbar-brand" title="home"  data-translate>PORTAL_NAME</a>
    

    As a side note, i don't think $route.reload() actually reloads the page, but only the route.

    0 讨论(0)
  • 2020-11-28 20:07

    Similar to Alexandrin's answer, but using $state rather than $route:

    (From JimTheDev's SO answer here.)

    $scope.reloadState = function() {
       $state.go($state.current, {}, {reload: true});
    }
    
    <a ng-click="reloadState()" ... 
    
    0 讨论(0)
  • 2020-11-28 20:08

    You can use the reload method of the $route service. Inject $route in your controller and then create a method reloadRoute on your $scope.

    $scope.reloadRoute = function() {
       $route.reload();
    }
    

    Then you can use it on the link like this:

    <a ng-click="reloadRoute()" class="navbar-brand" title="home"  data-translate>PORTAL_NAME</a>
    

    This method will cause the current route to reload. If you however want to perform a full refresh, you could inject $window and use that:

    $scope.reloadRoute = function() {
       $window.location.reload();
    }
    


    Later edit (ui-router):

    As mentioned by JamesEddyEdwards and Dunc in their answers, if you are using angular-ui/ui-router you can use the following method to reload the current state / route. Just inject $state instead of $route and then you have:

    $scope.reloadRoute = function() {
        $state.reload();
    };
    
    0 讨论(0)
  • 2020-11-28 20:08

    My solution to avoid the infinite loop was to create another state which have made the redirection:

    $stateProvider.state('app.admin.main', {
        url: '/admin/main',
        authenticate: 'admin',
        controller: ($state, $window) => {
          $state.go('app.admin.overview').then(() => {
            $window.location.reload();
          });
        }
      });
    
    0 讨论(0)
  • 2020-11-28 20:11

    I would suggest to refer the page. Official suggestion

    https://docs.angularjs.org/guide/$location

    0 讨论(0)
  • 2020-11-28 20:13
     location.reload(); 
    

    Does the trick.

    <a ng-click="reload()">
    
    $scope.reload = function()
    {
       location.reload(); 
    }
    

    No need for routes or anything just plain old js

    0 讨论(0)
提交回复
热议问题