angular $location服务的主要作用是用于获取当前url以及改变当前的url,也就是做搜索功能。具体看你们后端给你们的api,拼成适合的字符串
一. 获取url的相关方法:
以‘http://localhost/$location/21.1%20$location.html#/foo?name=bunny#myhash’这个路径为例:
1.获取当前完整的url路径:
$location.absUrl():
// http://localhost/$location/21.1%20$location.html#/foo?name=bunny#myhash
*2. 获取当前url路径(当前url#后面的内容,包括参数和哈希值):
$location.url();
// /foo?name=bunny#myhash
// /foo?name=bunny#myhash
*3. 获取当前url的子路径(也就是当前url#后面的内容,不包括参数):
$location.path()
// /foo
// /foo
4. 获取当前url的协议(比如http,https)
$location.protocol()
// http
// http
5. 获取当前url的主机名
$location.host()
// localhost
// localhost
6. 获取当前url的端口
$location.port()
// 80 (这里就是wamp的默认端口号)
// 80 (这里就是wamp的默认端口号)
*7. 获取当前url的哈希值
$location.hash()
// myhash
// myhash
*8. 获取当前url的参数的序列化json对象
$location.search()
// {"name":"bunny"}
// {"name":"bunny"}
二. 修改url的相关方法:
在上面讲到的所有获取url的8个方法,其中*开头的四个方法,可以传入参数进行修改url,在这种情况下,函数的返回值都是$location本身:
1.修改url的子路径(也就是当前url#后面的内容,不包括参数):
参数格式:字符串
$location.url('/foo2?name=bunny2&age=12#myhash2');
// http://localhost/$location/21.1%20$location.html#/foo2?name=bunny2&age=12#myhash2
// http://localhost/$location/21.1%20$location.html#/foo2?name=bunny2&age=12#myhash2
2. 修改url的子路径部分(也就是当前url#后面的内容,245)">$location.path('/foo2/foo3');
/foo2/foo3/?name=bunny2&age=12#myhash2