Angular: Passing params from $routeProvider to controller

前端 未结 4 1583
野性不改
野性不改 2020-12-13 01:54

I have multiple routes that invoke the same Controller and I would like to pass different variables to it.

// Example
$routeProvider.
  when(\'/a\', {
    te         


        
4条回答
  •  Happy的楠姐
    2020-12-13 02:44

    using $routeParams

    in Main js file

    routeApp.config(function($routeProvider) {
    $routeProvider
        .when('/home', {
            templateUrl: '../sites/./home.html',
            controller: 'StudentController'
        })
        .when('/viewStudents/:param1/:param2', {
            templateUrl: '../sites/./viewStudents.html',
            controller: 'StudentController'
        })
        .otherwise({
            redirectTo: '/'
        });
     });
    
     routeApp.controller('StudentController',['$filter','$routeParams', '$location',function($filter,$routeParams,$location){
       var StudentCtrl = this;  
       StudentCtrl.param1 = $routeParams.param1;
       StudentCtrl.param2 = $routeParams.param2;
     }]);
    

    calling from Home.html

     

    Welcome

    Students

提交回复
热议问题