简体   繁体   English

AngularJS整页刷新

[英]AngularJS full page refresh

I am using Laravel with Angular and need a full page refresh when I switch to a particular route. 我在Angular中使用Laravel,切换到特定路线时需要刷新整页。

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 模板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. 我想调用pageRefresh()可以,但是它会无限循环地刷新页面。 I only need it to do it once. 我只需要做一次。 Where am I going wrong? 我要去哪里错了?

I have also tried $route.reload(); 我也试过$route.reload(); but I need a full page reload, not just a view refresh. 但我需要重新加载整个页面,而不仅仅是刷新视图。

you are calling pageRefresh(); 您正在调用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 您必须具有一些逻辑/条件才能刷新,因此仅在该条件上调用pageRefresh()

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. 您可以做的是使用init()函数来初始化控制器。 and a reset() function which resets the value to original state. 一个reset()函数将值重置为原始状态。

now you should use these functions along with reload() to get the feel of full page refresh. 现在,您应该将这些功能与reload()以获得整页刷新的感觉。

Set a ng-click on your anchor which calls the pageRefresh function and pass a argument to this which is your desired url. 在锚点上设置ng-click,该锚点将调用pageRefresh函数,并将一个参数传递给该参数,这就是您所需的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. 然后,在重新加载后,使用$ locationChangeSuccess将$ location.path设置为url,您必须在此函数中检查是否是页面重新加载。 This way you won't get an infinite loop. 这样,您将不会陷入无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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