解析在AngularJS请求的URL的最佳方式
我有一个像网址:
http://www.something.com/project/edit/
987654321
什么是解析 987654321 使用AngularJS的URL的一部分的最好方法?有没有在任何角度辅助功能? (我preFER不使用jQuery)
What's the best way to parse the 987654321 part of the URL using AngularJS? Are there any helper functions within Angular? (I'd prefer to not use jQuery)
如果我理解正确的话,在AngularJS路由功能将无法正常工作,除非你使用#在URL中或支持HTML5模式。
If I understand it correctly, the routing functions within AngularJS won't work unless you use a # within the URL or enabled HTML5 mode.
我们需要解析此网址时第一次加载页面。
We need to parse this URL when the page is first loaded.
您可以使用的 $位置
服务的 $#位置路径()
功能:
You can use the $location
service's $location#path()
function:
# given: url = http://www.something.com/project/edit/987654321
$location.path().split('/').pop();
然而,这听起来像你有一个路由问题。查看角教程路由,它展示了如何正确使用 $在你的
路线。从教程: app.js
配置routeProvider
However, it sounds like you have a routing issue. Check out the Angular Tutorial on Routing, which shows how to correctly use $routeProvider
routes in your app.js
configuration. From the tutorial:
phonecatApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/phones/:phoneId', {
templateUrl: 'partials/phone-detail.html',
controller: 'PhoneDetailCtrl'
})
.otherwise( ...) //omitting remainder of cut/paste
所以你的 app.js
将有一个路由定义,如:
So your app.js
would have a route definition like:
ender2050App.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/project/edit/:id', {
templateUrl: 'partials/editor.html',
controller: 'EditorCtrl'
})
和控制器文件(即controllers.js)将有 EditorCtrl
:注射的 $ routeParams
服务访问 ID
变量。
And your controller file (i.e., controllers.js) would have the EditorCtrl
that injects the $routeParams
service to access the id
variable.
如果你需要一个更个性解析选项,查看 $解析
服务。
If you need a more customized parsing option, check out the $parse
service.