我目前在/ addDoc,我想要当前的路线.这是我的控制器:
app.controller('DocRegistrationController',[ '$http','$scope','$upload','$location',function($http,$scope,$upload,$location){ $scope.validation=function(){ alert($location.path()); } }
但是它返回空.我不想硬编码所有路线.我究竟做错了什么?
$location服务响应解析浏览器地址栏中的URL并使URL可用于您的APP.
因为您使用常规URL路径和搜索段,所以必须将$locationProvider html5Mode设置为true.
$locationProvider将使用hashbang作为默认模式.
如果未将html5Mode设置为true,则在尝试获取url路径时将获得空字符串.
然后在设置html5Mode之后使用$location服务获取url路径.
并编写自己的规则来处理路径.
假设您的完整网址如下所示:http://example.com/person/show/321
Main.js
angular.module("MyAPP",[],function($locationProvider){ $locationProvider.html5Mode(true); }); function MainController($location){ var pId = $location.path().split("/")[3]||"Unknown"; //path will be /person/show/321/,and array looks like: ["","person","show","321",""] console.log(pId); }
的index.html
<!DOCTYPE html> <html lang="en" ng-app="MyAPP"> <head> <Meta charset="utf-8"> <title>Angular test</title> </head> <body ng-controller="MainController"> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> <script src="js/main.js"></script> </body> </html>
希望这对你有所帮助.