【发布时间】:2023-03-11 06:37:01
【问题描述】:
我正在尝试使用角度 routeProvider 和 routeParams 来获取 id 和 name 参数。 这是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);
}
]);
还有 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>
这不是 SPA,因此我没有在 when 函数中设置 templateUrl 和控制器。我只需要 URL 参数。我已经包含了 angular 和 angular-route 库
【问题讨论】:
-
将控制器添加到您的 when 对象是否可以解决问题? IE。 when("/test/:id",{ 控制器:'testCtrl' })
-
不,像以前一样返回未定义
标签: javascript angularjs html route-provider