最近,项目开发正在进行时,心有点燥,许多东西没来得及去研究,今天正想问题呢,同事问到如何获取url中的参数,我一时半会还真没想起来,刚刚特意研究了一下,常用的方法就以下几种:
var absurl = $location.absUrl(); //http://88:8100/#/homePage?id=10&a=100
var url = $location.url(); // /homePage?id=10&a=100
var pathUrl = $location.path() ///homePage
var protocol = $location.protocol(); //http
var localhost = $location.host(); //88
var port = $location.port(); //8100
var hash = $location.hash() //http://088
var search = $location.search(); //{id: "10", a: "100"}
$location.search().name;
$location.search()['name'];
需要在项目中注入$locationProvider服务
1 var searchApp = angular.module('searchApp', []);
2
3 searchApp.config(['$locationProvider', function($locationProvider) {
4
5 $locationProvider.html5Mode(true);
6
7 }]);
8
9 searchApp.controller('MainCtrl', ['$scope', '$location', function($scope, $location) {
10
11 if ($location.search().keyword) {
12
13 $scope.keyword = $location.search().keyword;
14
15 }
16
17 }]);
url = https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=%E5%A8%83%E5%93%88%E5%93%88
console.log(window.location.href ); // "https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=%E5%A8%83%E5%93%88%E5%93%88"
console.log(window.location.host); // "www.baidu.com"
console.log(window.location.pathname); // "/s"
console.log(window.location.protocol); // "https:"
console.log(window.location.search); // "?ie=utf-8&f=3&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=%E5%A8%83%E5%93%88%E5%93%88"