更新时间:2022-06-14 23:12:12
使用 ngRoute
从 URL 获取参数.这意味着您需要包含 angular-route.js 在您的应用程序中作为依赖项.有关如何在官方 ngRoute 文档 中执行此操作的更多信息.
To get parameters from URL with ngRoute
. It means that you will need to include angular-route.js in your application as a dependency. More information how to do this on official ngRoute documentation.
问题的解决方案:
// You need to add 'ngRoute' as a dependency in your app
angular.module('ngApp', ['ngRoute'])
.config(function ($routeProvider, $locationProvider) {
// configure the routing rules here
$routeProvider.when('/backend/:type/:id', {
controller: 'PagesCtrl'
});
// enable HTML5mode to disable hashbang urls
$locationProvider.html5Mode(true);
})
.controller('PagesCtrl', function ($routeParams) {
console.log($routeParams.id, $routeParams.type);
});
如果您没有启用 $locationProvider.html5Mode(true);
.网址将使用 hashbang(/#/
).
If you don't enable the $locationProvider.html5Mode(true);
. Urls will use hashbang(/#/
).
有关路由的更多信息可以在官方 angular $route API 文档中找到>.
More information about routing can be found on official angular $route API documentation.
旁注: 这个问题是回答如何使用 ng-Route 实现这一点,但我建议使用 ui-Router 用于路由.它更灵活,提供更多功能,文档很棒,被认为是 angular 的***路由库.
Side note: This question is answering how to achieve this using ng-Route however I would recommend using the ui-Router for routing. It is more flexible, offers more functionality, the documentations is great and it is considered the best routing library for angular.