簡體   English   中英

刷新頁面時,AngularJS控制器不會觸發

[英]AngularJS Controller does not fire when I refresh the page

當我進入“報告”頁面時,控制器將觸發並在我的services.js文件中執行一些代碼。 服務從數據庫返回信息,該信息用於顯示/隱藏HTML中的標記。 但是,如果刷新“搜索”頁面,控制器不會啟動,並且“報告”頁面顯示所有標簽,則隱藏某些標簽的功能將不起作用

我的代碼如下。

route.js

控制器的名稱為: reportsCtrl

 application.config(['$routeProvider', '$locationProvider',
 function($routeProvider, $locationProvider) {
 $routeProvider
    .when('/dashboard', {
        templateUrl: window._ctxpath + '/resources/partials/dashboard.html',
        controller: 'dashboardCtrl'
    })
    .when('/search', {
        templateUrl: window._ctxpath + '/resources/partials/search.html',
        controller: 'searchCtrl'
    })
    // REPORTS SERVER REPORTS.
    .when('/reports', {
        templateUrl: window._ctxpath + '/resources/partials/reports.html',
        controller: 'reportsCtrl'
    })
    // REPORTS (DASHBOARD REPORTS).
    .when('/dashboard/report', {
        templateUrl: window._ctxpath + '/resources/partials/reports/unloads.html',
        controller: 'reportunloadsCtrl'
    })
    .when('/dashboard/report/unloads', {
        templateUrl: window._ctxpath + '/resources/partials/reports/unloads.html',
        controller: 'reportunloadsCtrl'
    })
    .when('/dashboard/report/carriers', {
        templateUrl: window._ctxpath + '/resources/partials/reports/metrics.html',
        controller: 'reportcarriersCtrl'
    })
    .when('/dashboard/report/vendors', {
        templateUrl: window._ctxpath + '/resources/partials/reports/metrics.html',
        controller: 'reportvendorsCtrl'
    })
    .when('/dashboard/report/items', {
        templateUrl: window._ctxpath + '/resources/partials/reports/metrics.html',
        controller: 'reportitemsCtrl'
    })
    .when('/dashboard/report/exceptions', {
        templateUrl: window._ctxpath + '/resources/partials/reports/metrics.html',
        controller: 'reportexceptionsCtrl'
    })
    .when('/dashboard/report/loadtypes', {
        templateUrl: window._ctxpath + '/resources/partials/reports/loadtypes.html',
        controller: 'reportloadtypesCtrl'
    })
    .when('/dashboard/report/pallettypes', {
        templateUrl: window._ctxpath + '/resources/partials/reports/metrics.html',
        controller: 'reportpallettypesCtrl'
    })
    .otherwise({
        redirectTo: '/dashboard'
    });
$locationProvider.html5Mode(false);
}])

這是實際的控制器,它在reportsReportServer.js文件中

var reportsCtrl = application.controller('reportsCtrl', ['$rootScope', '$scope', '$context', '$reportVisibilitySvc',
function($rootScope, $scope, $ctx, $reportVisibilitySvc) {

    var testField = null;
    testField = "I am in the reports (form report server) controller";
    console.log(testField);

    $rootScope.getReportPermissions = function() {
        if( $ctx.dock ) {
            $ctx.rpt = null;
            let month = null;
            let endDate = $ctx.range.split(' - ')[1];
            let date = new Date(endDate);
            month = date.getMonth() + 1;
            $ctx.rpt = $reportVisibilitySvc.Values(month, $ctx.dock);
        }
    };
    $rootScope.getReportPermissions();
}]);

這是該服務的代碼...位於一個名為services.js的文件中

//This service is used to return the information for the database and see what report in the Reports page should be hidden.
var $reportVisibilitySvc = application.service('$reportVisibilitySvc', [ 'baseService', '$rootScope', '$resource',
function(baseService, $rootScope, $resource) {

    var $rpt = $resource(window._ctxpath+'/rpt/:p1/:p2',
            { p1 : '@p1', p2 : '@p2' },
            {savem : {method : 'POST', isArray : true}
    });

    this.Values = function(month, dock) {
        if (dock !== null) {
            console.log('I am in the service.js file, in the $reportVisibilitySvc service, in the this.Values function')
            return $rpt.save({p1 : 'values', p2 : month}, dock, function(o, head) {});
        }
        else {
            return null;
        }
    };
}]);

再次。 當我進入報告頁面時,控制器和服務便會執行,但如果我通過單擊Google Chrome上的“刷新”按鈕刷新頁面,我也希望它觸發。

我找到了一個解決方案:我注意到刷新頁面后觸發了contextCtrl控制器,所以我要做的是將在reportCtrl中找到的代碼放入contextCtr控制器中,以再次調用$ reportVisibilitySvc服務。

if ( sessionStorage.getItem('LV_CTX_DOCK') && $ctx.range ) {
    $ctx.rpt = null;
    $ctx.dock = null;
    let month = null;
    let endDate = $ctx.range.split(' - ')[1];
    let date = new Date(endDate);
    month = date.getMonth() + 1;
    // get the dock
    $ctx.dock = sessionStorage.getItem('LV_CTX_DOCK');
    // get the data from the database to see what reports are hidden/visible
    $ctx.rpt = $reportVisibilitySvc.Values(month, $ctx.dock);
}

這按我的需要工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM