Angular: Passing params from $routeProvider to controller

前端 未结 4 1580
野性不改
野性不改 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条回答
  • 2020-12-13 02:37

    You can use resolve and $route.currrent.params.test to pass the parameter like this:

    $routeProvider
      .when('/a', {
        templateUrl: 'view.html',
        controller: 'MainCtrl',
        resolve: {
            test: function ($route) { $route.current.params.test = true; }
        }
      })
      .when('/b', {
        templateUrl: 'view.html',
        controller: 'MainCtrl',
        resolve: {
            test: function ($route) { $route.current.params.test = false; }
        }
      })
    

    Then in your controller you can access it from the $routeParams:

    app.controller('MainCtrl', function($scope, $routeParams) {
      $scope.test = $routeParams.test;
    })
    

    http://plnkr.co/edit/ct1ZUI9DNqSZ7S9OZJdO?p=preview

    0 讨论(0)
  • 2020-12-13 02:43

    The most natural way to do this is doing what you would normally do when you want to load a page with parameters: use query parameters: http://yoururl.com?param1=value&param2=value

    ngRoute comes with the service $routeParams which you can inject in your controller. Now you can simply retrieve the values like this $routeParams.param1.

    Another way to do this is to retrieve the path with $location.path and set the variable there.

    0 讨论(0)
  • 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

     <div class="container">
       <h2> Welcome </h2>
       <a href="#/viewStudents/myname/somevalue2">Students</a>
    </div>
    
    0 讨论(0)
  • 2020-12-13 02:53

    Routing:

    $routeProvider.
      when('/a', {
        templateUrl: 'test.html',
        controller: 'MyController',
        paramExample: 'exampleA'
      }).
      when('/b', {
        templateUrl: 'test.html',
        controller: 'MyController',
        paramExample: 'exampleB'
    });
    

    Access: inject $route in your controller then use this

     app.controller('MyController', ['$scope', '$route', function ($scope, $route) {
          var paramValue = $route.current.$$route.paramExample;
          console.log(paramValue); 
     }
    
    0 讨论(0)
提交回复
热议问题