Angular学习-$location
来源:互联网 发布:java 生成excel 编辑:程序博客网 时间:2024/05/18 01:36
序
angularjs location封装了浏览器原生的location对象,提供了很多方便的方法。
本文的angularjs版本是1.2.2
location是什么?
location是一个关于当前网页地址的对象,从属于window之下,当然,window之下的对象在不引起歧义或者混淆的情况下,一般是可以省去window的。location本身是一个js对象,包含一些属性和方法。具体可以参考w3school 。
angular location
angular中的location是一个service,写作$location。
getter和setter方法
getter方法用于获取对象的私有属性,setter用于设置对象的私有属性。
js中是不存在私有变量的,但是通过一些命名方式(如私有变量名以双下划线开始)约定,可以人为的规定某些变量为私有变量。
正因为js本身不支持私有变量,所以对象的属性我们都可以通过 object.variable的方式读取。getter和setter方法只是我们为了让js对象的操作更加规范。例如,可以在setter方法中做一些规则校验,在getter方法中做一些初始化,等等。
Methods
注:如果方法没有参数说明或者示例,表示不需要参数。
absUrl()
getter:返回完整url。
hash()
getter:返回hash段。
setter:设置hash段并返回
host()
getter:返回host。
path()
getter:返回路径。http://a.com/123, path就是123。
setter:设置路径。$location.path(‘/124’)。
port()
getter:返回端口号。
protocol()
getter:返回协议名,如https。
search()
search指的是url的query部分。
window.location.search会返回包括?的部分,个人认为十分不方便解析。
假设url为:http://a.com?name=remind&gender=male,
getter: 返回search对象
getter返回对象:{name:’remind’,gender:’male’}。
setter: 设置query,有两种方式。
1.
这种方法是部分更新(part update)。
如果在一个angular周期中,只更新其中一个,如只执行
$location.search(‘name’,’remind’)
那么gender会自动设置成空,如:http://a.com?name=remind&gender
2.
那么gender会被删除,如 : http://a.com?name=remind
可以发现,设置方法类似jquery设置css的方式,对比一下w3school.
遗憾的是search不支持查询某个特定值,例如
Events
当调用$location.path(‘xxx’)使得地址变更时,以下事件会被广播出去,在angular的作用域(scope)中可以监听到此类事件。
$locationChangeStart
在$location的path change之后,url change发生改变之前,此事件被广播出去。url change可以被阻止,方法是调用此事件的preventDefault方法。
$locationChangeSuccess
当url change之后,此event被广播出去。
参考
angular 1.2.2 location service
- Angular学习-$location
- ANGULAR学习笔记(三十一)-$LOCATION(1)
- [转]angular学习笔记(三十一)-$location(1)
- Angular location
- angular location replace
- angular $location服务
- Angular之$location
- Angular $location 获取 url 参数
- angular之$location基本用法
- angular $location服务的配置
- Angular学习(十五)——Route和$location/与服务器交互
- angular js 使用$location问题整理
- angular的location服务的使用
- angular利用$location实现搜索功能。
- angular js Laravel location.href="网址"
- angular学习
- Angular学习
- angular学习
- 解决Jdeveloper中CVS Check In后乱码问题
- 关于Debug和Release
- 远程调试weblogic应用服务器中的程序
- .net 使用模板页,JS使用document.getElementById("")得到null的解决办法
- 触摸屏幕实现拖尾效果
- Angular学习-$location
- C/C++中判断GCC版本
- How to Set Up a USB Key to Install CentOS
- 进程间通信之消息队列msg queue
- border模拟小三角形
- Jdeveloper 导出Java Doc
- cssSprites,如何来规划?
- 有趣的机器学习:最简明入门指南
- 快速排序和堆排序