Posted in Javascript onAugust 02, 2018
最近,项目开发正在进行时,心有点燥,许多东西没来得及去研究,今天正想问题呢,同事问到如何获取url中的参数,我一时半会还真没想起来,刚刚特意研究了一下,常用的方法就以下几种:
1.获取当前完整的url路径
var absurl = $location.absUrl(); //http://88:8100/#/homePage?id=10&a=100
2. 获取当前url路径(当前url#后面的内容,包括参数和哈希值)
var url = $location.url(); // /homePage?id=10&a=100
3. 获取当前url的子路径(也就是当前url#后面的内容,不包括参数)
var pathUrl = $location.path() ///homePage
4.获取当前url的协议(比如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的参数的序列化json对象
var search = $location.search(); //{id: "10", a: "100"}
9. 获取url参数
$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中获取地址栏参数的方法(附加)
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"
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
详解Angular中通过$location获取地址栏的参数
- Author -
半指温柔乐声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@