user2280016
user2280016

Reputation: 1850

How to pass parameters to a controller when using route?

In my application, am loading views using ngRoute,

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

I have a search Panel where users can search reports, and when selecting a report, it should route to a different page and load the report inside an iframe. Here is my code when the user select the report,

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

I have defined a constant in the config which changes dynamically based on the report selection

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

Here i need to pass the 'Report' variable to the ReportViewerControl when the user select the report,

Here is my 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);
    }
}
]);

Upvotes: 3

Views: 305

Answers (1)

vignesh
vignesh

Reputation: 151

you are using ui-router for confiuring routes ,but below in ReportController you are using $routeParams.I hope you have to use $stateParams for that.

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

also you have to pass the params from url or in method like this

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

Then you can navigate to it like so:

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

Or:

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

Upvotes: 2

Related Questions