AngularJs: Reload page

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

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

相关标签:
13条回答
  • 2020-11-28 19:54

    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.

    0 讨论(0)
  • 2020-11-28 19:54
    <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.

    0 讨论(0)
  • 2020-11-28 19:55

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

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

    Angular 2+

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

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

    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.

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

    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.

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