使用路由时如何将参数传递给控制器​​?

How to pass parameters to a controller when using route?

在我的应用程序中,我正在使用 ngRoute 加载视图,

 .state('ReportViewer', {
            url: "/ReportViewer",
            controller: 'ReportViewerControl',
            templateUrl: "views/Report_viewer.html"
        })

我有一个搜索面板,用户可以在其中搜索报告,当 select 生成报告时,它应该路由到不同的页面并将报告加载到 iframe 中。这是我的代码,当用户 select 报告时,

 $scope.goReport = function(report){
         $state.go('ReportViewer');
        }

我在配置中定义了一个常量,它会根据报告动态变化 selection

   .constant('Digin_ReportViewer','http://192.226.192.147:8080/api/repos/%3Ahome%3Aadmin%')

这里我需要在用户select报告时将'Report'变量传递给ReportViewerControl

这是我的 ReportViewerControl

 routerApp.controller('ReportViewerControl', ['$scope', '$routeParams','Digin_ReportViewer',function($scope, $routeParams,Digin_ReportViewer) {

    //here i need to append the report url ,
    $scope.reportURL = Digin_ReportViewer+routeParams.report ; 
  $scope.trustSrc = function(src) {
       return $sce.trustAsResourceUrl(src);
    }
}
]);

您正在使用 ui-router 来配置路由,但是在 ReportController 下面您正在使用 $routeParams.I 希望您必须为此使用 $stateParams。

 routerApp.controller('ReportViewerControl', ['$scope', '$stateParams','Digin_ReportViewer',function($scope, $stateParams,Digin_ReportViewer) {

    //here i need to append the report url ,
    $scope.reportURL = Digin_ReportViewer+stateParams.report ; 
  $scope.trustSrc = function(src) {
       return $sce.trustAsResourceUrl(src);
    }
}
]);

你还必须从 url 或像这样的方法中传递参数

.state('ReportViewer', {
            url: "/ReportViewer",
            controller: 'ReportViewerControl',
            templateUrl: "views/Report_viewer.html",
            params: ['report']
        })

然后您可以像这样导航到它:

$scope.goReport = function(report){
         $state.go('ReportViewer', { 'report':'monthly' });
}

或者:

var result = { 'report':'monthly' };
  $state.go('ReportViewer', result);