最近,項目開發(fā)正在進行時,心有點燥,許多東西沒來得及去研究,今天正想問題呢,同事問到如何獲取url中的參數(shù),我一時半會還真沒想起來,剛剛特意研究了一下,常用的方法就以下幾種:
1.獲取當前完整的url路徑
var absurl = $location.absUrl(); //http://88:8100/#/homePage?id=10&a=100
2. 獲取當前url路徑(當前url#后面的內容,包括參數(shù)和哈希值)
var url = $location.url(); // /homePage?id=10&a=100
3. 獲取當前url的子路徑(也就是當前url#后面的內容,不包括參數(shù))
var pathUrl = $location.path() ///homePage
4.獲取當前url的協(xié)議(比如http,https)
var protocol = $location.protocol(); //http
5.獲取主機名
var localhost = $location.host(); //88
6.獲取當前url的端口
var port = $location.port(); //8100
7.獲取當前url的哈希值
var hash = $location.hash() //http://088
8.獲取當前url的參數(shù)的序列化json對象
var search = $location.search(); //{id: "10", a: "100"}9. 獲取url參數(shù)
$location.search().name;$location.search()['name'];
10.注意問題
如果是這樣的地址:http://lele.sina.com?name=haha
需要在項目中注入$locationProvider服務
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;}}]);11.js中獲取地址欄參數(shù)的方法(附加)
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%88console.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"
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答
圖片精選