agular $location 获取url的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 带#号的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;
}
}]);
坚持原创技术分享,您的支持将鼓励我继续创作!
//