简体   繁体   中英

Angular routeParams returns an empty object

I'm trying to use the angular routeProvider and routeParams to get the id and name parameters. Here's the js:

angular.module("test",["ngRoute"])
 .config(["$routeProvider",function(a){
     a.when("/test/:id",{}).
       when("/tests/:name",{});
     }
 ]).controller("testCtrl",["$scope","$routeParams",function(a,b){
     a.result = b.name; //console.log(b.id);
 }
]);

And the HTML

<html ng-app="test">
...
<body ng-controller="testCtrl">
<p>Test ID</p>
<a href="/test/1">1</a>
<a href="/test/2">2</a>


<p>Test Name</p>
<a href="/tests/test1">Test1</a>
<a href="/tests/test1">Test2</a>
<a href="/tests/test1">Test3</a>


<p>RouteParams Result</p>
<p>{{ result }}</p>
</body>

This is not a SPA and hence I'm not setting the templateUrl and controller in when function. I only need the URL params. I have included the angular and angular-route libs

Here's the plunkr just in case

 a.when("/test/:id",{}).
   when("/tests/:name",{
     controller: "testCtrl"
   });
 }

You need to specify controller to the current state in order to get routeParams.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM