angular可以使用$location方法接收(获取)URL参数,Location对象包含有关当前URL的信息。Location对象是Window对象的一个部分,可通过window.location属性来访问。
angular接收URL参数:// 带#号的url,看?号的url,见下面
url = http://qiaole.sinaapp.com?#name=cccccc
$location.absUrl();
// http://qiaole.sinaapp.com?#name=cccccc
$location.host();
// qiaole.sinaapp.com
$location.port();
// 80
$location.protocol();
// http
$location.url();
// ?#name=cccccc
// 获取url参数
$location.search().name;
// or
$location.search()['name'];
// 注:如果是这样的地址:http://qiaole.sinaapp.com?name=cccccc
var searchApp = angular.module('searchApp', []);
searchApp.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode(true);
}]);
searchApp.controller('MainCtrl', ['$scope', '$location', function($scope, $location) {
if ($location.search().keyword) {
$scope.keyword = $location.search().keyword;
}
}]);
angular的$location方法获取URL参数:
获取url的相关方法:
以 ‘http://localhost/location/21.1 location/21.1%20location/21.1location.html#/foo?name=bunny#myhash’ 这个路径为例:
1. 获取当前完整的url路径:location.absUrl()://http://localhost/ location.absUrl()://http://localhost/location.absUrl():
//http://localhost/location/21.1%20$location.html#/foo?name=bunny#myhash
*2. 获取当前url路径(当前url#后面的内容,包括参数和哈希值):$location.url();
// /foo?name=bunny#myhash
*3. 获取当前url的子路径(也就是当前url#后面的内容,不包括参数):$location.path()
// /foo
4. 获取当前url的协议(比如http,https)$location.protocol()
// http
5. 获取当前url的主机名$location.host()
// localhost
6. 获取当前url的端口$location.port()
// 80 (这里就是wamp的默认端口号)
*7. 获取当前url的哈希值$location.hash()
// myhash
*8. 获取当前url的参数的序列化json对象$location.search()
// {“name”:“bunny”}