刷新页面时,AngularJS Controller不会触发

时间:2018-01-29 20:58:59

标签: javascript angularjs

当我进入Reports页面时,控制器将触发并执行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;
        }
    };
}]);

再次。当我进入报告页面时,控制器和服务会执行,但是如果我通过点击谷歌浏览器上的刷新按钮刷新页面,我也希望它能够触发,有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案:我注意到在刷新页面后contextCtrl控制器被触发,所以我做的是将contextCtr控制器中的reportsCtrl中找到的代码再次调用$ 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);
}

这就像我需要的那样。