用anglajs怎样改变html的url,angularjs如何获取url参数?

Angular中通过$location获取URL参数。$location服务负责解析浏览器地址栏中的URL(基于window.location),以便你的应用可以访问它。 这是一个双向同步机制 —— 对地址栏URL的任何修改都会被映射到$location服务中,对$location的任何修改也同样会被映射到地址栏。

用anglajs怎样改变html的url,angularjs如何获取url参数?_第1张图片

Angular中通过$location获取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

// "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 );

console.log(window.location.host); // "www.baidu.com"

console.log(window.location.pathname); // "/s"

console.log(window.location.protocol); // "https:"

// "?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.search);

你可能感兴趣的:(用anglajs怎样改变html的url,angularjs如何获取url参数?)