135
<a ng-href="#" class="navbar-brand" title="home" data-translate>PORTAL_NAME</a>

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

Alexandrin Rus
  • 4,470
  • 2
  • 16
  • 29
user880386
  • 2,737
  • 7
  • 33
  • 41

13 Answers13

269

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();
};
Alexandrin Rus
  • 4,470
  • 2
  • 16
  • 29
52

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.

Florian F.
  • 4,700
  • 26
  • 50
  • 2
    I recommend using `$window` instead of `window`. – Jeroen Jun 03 '16 at 08:04
  • Thanks for your contribution. Would you explain us why ? – Florian F. Jun 03 '16 at 17:47
  • 1
    Yes, sorry. It's best explained by [the `$window` docs](https://docs.angularjs.org/api/ng/service/$window), mainly it's because `reloadPage` would be (better) testable when it uses `$window`. – Jeroen Jun 03 '16 at 17:56
23
 location.reload(); 

Does the trick.

<a ng-click="reload()">

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

No need for routes or anything just plain old js

user3806549
  • 1,428
  • 1
  • 17
  • 25
14

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()" ... 
Community
  • 1
  • 1
Dunc
  • 18,404
  • 6
  • 86
  • 103
9

If using Angulars more advanced ui-router which I'd definitely recommend then you can now simply use:

$state.reload();

Which is essentially doing the same as Dunc's answer.

5

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();
      });
    }
  });
Gucu112
  • 877
  • 10
  • 12
2

Angular 2+

I found this while searching for Angular 2+, so here is the way:

$window.location.reload();
neowinston
  • 7,584
  • 10
  • 52
  • 83
Nitin Jadhav
  • 6,495
  • 1
  • 46
  • 48
2

This can be done by calling the reload() method in JavaScript.

location.reload();
1

It's easy enough to just use $route.reload() (don't forget to inject $route into your controller), but from your example you could just use "href" instead of "ng-href":

<a href=""  class="navbar-brand" title="home"  data-translate>PORTAL_NAME</a>

You only need to use ng-href to protect the user from invalid links caused by them clicking before Angular has replaced the contents of the {{ }} tags.

spikeheap
  • 3,827
  • 1
  • 32
  • 47
1

On Angular 1.5 - after trying some of the above solutions wanting to reload only the data with no full page refresh, I had problems with loading the data properly. I noticed though, that when I go to another route and then I return back to the current, everything works fine, but when I want to only reload the current route using $route.reload(), then some of the code is not executed properly. Then I tried to redirect to the current route in the following way:

$scope.someFuncName = function () {
    //go to another route
    $location.path('/another-route');
};

and in the module config, add another when:

.config(['$routeProvider', function($routeProvider) {
     $routeProvider.when('/first-page', {
         templateUrl: '/first-template',
         controller: 'SomeCtrl'
     }).when('/another-route', {//this is the new "when"
         redirectTo: '/first-page'
     });
}])

and it works just fine for me. It does not refresh the whole page, but only causes the current controller and template to reload. I know it's a bit hacky, but that was the only solution I found.

Oleg
  • 9,341
  • 2
  • 43
  • 58
vivanov
  • 1,422
  • 3
  • 21
  • 29
  • an easier way might be `var reload = $location.url(); $location.url(''); $timeout(function() { $location.url(reload); });`; no need to create random routes. But it'd be interesting to see what code doesn't re-evaluate for you. It looks like even `redirectTo`s and `resolve` functions [re-execute on a normal `$route.reload()`](http://jsfiddle.net/502jz9fx/1/) (look at the console.log's). – Hashbrown Sep 24 '18 at 01:31
1
<a title="Pending Employee Approvals" href="" ng-click="viewPendingApprovals(1)">
                    <i class="fa fa-user" aria-hidden="true"></i>
                    <span class="button_badge">{{pendingEmployeeApprovalCount}}</span>
                </a>

and in the controller

 $scope.viewPendingApprovals = function(type) {
                if (window.location.hash.substring(window.location.hash.lastIndexOf('/') + 1, window.location.hash.length) == type) {
                    location.reload();
                } else {
                    $state.go("home.pendingApproval", { id: sessionStorage.typeToLoad });
                }
            };

and in the route file

.state('home.pendingApproval', {
        url: '/pendingApproval/:id',
        templateUrl: 'app/components/approvals/pendingApprovalList.html',
        controller: 'pendingApprovalListController'
    })

So, If the id passed in the url is same as what is coming from the function called by clicking the anchor, then simply reload, else folow the requested route.

Please help me improve this answer, if this is helps. Any, suggestions are welcome.

AMRESH PANDEY
  • 195
  • 1
  • 10
0

This can be done by calling the reload() method of the window object in plain JavaScript

window.location.reload();
KevinG
  • 882
  • 3
  • 16
  • 33
Skillz
  • 308
  • 4
  • 10
0

I would suggest to refer the page. Official suggestion

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

enter image description here

Sandeep M
  • 248
  • 3
  • 6