最近,项目开发正在进行时,心有点燥,许多东西没来得及去研究,今天正想问题呢,同事问到如何获取url中的参数,我一时半会还真没想起来,刚刚特意研究了一下,常用的方法就以下几种:
1.获取当前完整的url路径
var absurl = $location.absUrl(); //http://88:8100/#/homePage"htmlcode">var url = $location.url(); // /homePage"htmlcode">var pathUrl = $location.path() ///homePage4.获取当前url的协议(比如http,https)
var protocol = $location.protocol(); //http5.获取主机名
var localhost = $location.host(); //886.获取当前url的端口
var port = $location.port(); //81007.获取当前url的哈希值
var hash = $location.hash() //http://0888.获取当前url的参数的序列化json对象
var search = $location.search(); //{id: "10", a: "100"}9. 获取url参数
$location.search().name; $location.search()['name'];10.注意问题
如果是这样的地址:http://lele.sina.com"htmlcode">
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"https://www.baidu.com/s" 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); // ""以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。