【问题标题】:How to pass parameters to a controller when using route?使用路由时如何将参数传递给控制器​​?
【发布时间】:2015-08-18 15:34:27
【问题描述】:

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

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

我有一个搜索面板,其中用户可以搜索报告,并且在选择报表时,它应该在不同的页面路由并加载iframe内的报表。这是用户选择报告时的代码,

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

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

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

这里我需要在用户选择报告时将“报告”变量传递给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);
    }
}
]);

【问题讨论】:

    标签: javascript angularjs html routes


    【解决方案1】:

    你正在使用 ui-router 来配置路由,但在 ReportController 下面你正在使用 $routeParams。我希望你必须为此使用 $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);
    

    【讨论】:

    • TypeError: a.match 不是函数
    • 您能描述一下您面临的问题或错误吗?
    • 这是错误,出现未捕获错误:[$injector:modulerr] 无法实例化模块 DiginRt,原因是:TypeError: a.match is not a function at k (cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.13/…)跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2017-01-12
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2012-09-20
    相关资源
    最近更新 更多