【问题标题】:AngularJS full page refreshAngularJS 整页刷新
【发布时间】:2015-07-15 14:32:27
【问题描述】:

我将 Laravel 与 Angular 结合使用,当我切换到特定路线时需要刷新整个页面。

点击此链接时

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

那么我的 Angular 应用路由看起来像这样

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

模板page-content.html

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

这会动态获取页面,如下面的控制器所示

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;
        }
    });

}]);

我认为调用 pageRefresh() 会起作用,但它会在无限循环中刷新页面。我只需要它做一次。我哪里错了?

我也尝试过$route.reload();,但我需要重新加载整个页面,而不仅仅是刷新视图。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    您正在从控制器初始化调用pageRefresh();,这导致无限循环..

    你必须有一些刷新的逻辑/条件,所以只在那个条件下调用pageRefresh()

    当您通过锚点访问特定链接时,它会刷新整页

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

    编辑

    只是根据评论提出的建议。您可以做的是使用 init() 函数来初始化控制器。和一个 reset() 函数将值重置为原始状态。

    现在您应该将这些函数与reload() 一起使用,以获得整页刷新的感觉。

    【讨论】:

      【解决方案2】:

      在调用 pageRefresh 函数的锚点上设置 ng-click 并将参数传递给 this ,这是您想要的 url。然后在重新加载后使用 $locationChangeSuccess 将 $location.path 设置为 url,您还必须在此函数中检查它是否是页面重新加载。这样你就不会陷入无限循环。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-20
        • 2017-02-21
        • 2013-07-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多