欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Angular中通過$location獲取地址欄的參數(shù)

 更新時間:2018年08月02日 11:41:05   作者:半指溫柔樂  
這篇文章主要介紹了詳解 Angular中通過$location獲取地址欄的參數(shù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近,項目開發(fā)正在進(jìn)行時,心有點(diǎn)燥,許多東西沒來得及去研究,今天正想問題呢,同事問到如何獲取url中的參數(shù),我一時半會還真沒想起來,剛剛特意研究了一下,常用的方法就以下幾種:

1.獲取當(dāng)前完整的url路徑

var absurl = $location.absUrl(); //http://88:8100/#/homePage?id=10&a=100

2. 獲取當(dāng)前url路徑(當(dāng)前url#后面的內(nèi)容,包括參數(shù)和哈希值)

var url = $location.url(); // /homePage?id=10&a=100

3. 獲取當(dāng)前url的子路徑(也就是當(dāng)前url#后面的內(nèi)容,不包括參數(shù))

var pathUrl = $location.path() ///homePage

4.獲取當(dāng)前url的協(xié)議(比如http,https)

var protocol = $location.protocol(); //http

5.獲取主機(jī)名

var localhost = $location.host(); //88

6.獲取當(dāng)前url的端口

var port = $location.port(); //8100

7.獲取當(dāng)前url的哈希值

var hash = $location.hash() //http://088 

8.獲取當(dāng)前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服務(wù)

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%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"

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論