简体   繁体   中英

AngularJS full page refresh

I am using Laravel with Angular and need a full page refresh when I switch to a particular route.

When this link is clicked

<a href="admin/pages/{{page.id}}/content">Manage Content</a>

Then my angular app route looks like this

.when(baseUrl + '/admin/pages/:id/content', {
    templateUrl: 'page-content.html',
    controller: 'PageContentController'
});

The template page-content.html

<ng-include src="getEditView()"></ng-include>

This fetches the pages dynamically, as you can see in the controller below

Pages.controller('PageContentController', [
    '$scope', '$window', '$routeParams', '$location', 'dataService',
    function($scope, $window, $routeParams, $location, dataService) {

    $scope.id = $routeParams.id;

    pageRefresh();

    function pageRefresh() = {$window.location.reload()}

    dataService.one('pages', $scope.id).then(function(response){

        $scope.page = response;

        $scope.getEditView = function() {
            return 'templates/' + $scope.page.edit_template;
        }
    });

}]);

I figured calling the pageRefresh() would work, but it refreshes the page in an infinite loop. I only need it to do it once. Where am I going wrong?

I have also tried $route.reload(); but I need a full page reload, not just a view refresh.

you are calling pageRefresh(); from controller initiation which is causing infinte loop..

you must be having some logic/conditon for refresh, so call pageRefresh() only on that conditon

also whenever you are going to a particular link thru anchor it will be full page refresh

<a href="admin/pages/{{page.id}}/content">Manage Content</a>

EDIT

just a suggestion as per the comment. What you can do is have init() function which initializes the controller. and a reset() function which resets the value to original state.

now you should use these functions along with reload() to get the feel of full page refresh.

Set a ng-click on your anchor which calls the pageRefresh function and pass a argument to this which is your desired url. Then use $locationChangeSuccess to set the $location.path to the url after you have reloaded, you have to check if it was a page reload or not also in this function. This way you won't get an infinite loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM