Angular: Passing params from $routeProvider to controller

前端 未结 4 1585
野性不改
野性不改 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: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); 
     }
    

提交回复
热议问题