$watch $apply and $evalAsync vs $timeout

$watch

$scope对象上的$watch方法会给Angular事件循环内的每个$digest调用装配一个脏值检查。如果在表达式上检测到变化, Angular总是会返回$digest循环。

$watch函数本身接受两个必要参数和一个可选的参数:

  1.  watchExpression
    watchExpression可以是一个作用域对象的属性,或者是一个函数。在$digest循环中的每个$digest调用都会涉及它。如果watchExpression是一个字符串, Angular会在$scope上下文中对它求值。如果它是一个函数,那么Angular会认为它会返回应该被监控的值。
  2.  listener/callback
    作为回调的监听器函数,它只会在watchExpression的当前值与先前值不相等(除了首次运行初始化期间)时调用。
  3.  objectEquality(可选)
    objectEquality是一个进行比较的布尔值,用来告诉Angular是否检查严格相等。$watch函数会给监听器返回一个注销函数,我们可以调用这个注销函数来取消Angular对当前值的监控。

213419-20161026082143468-2123093966.png

213419-20161026083323093-585544494.png

$evalAsync vs $timeout

http://stackoverflow.com/questions/17301572/angularjs-evalasync-vs-timeout
To summarize:
if code is queued using $evalAsync from a directive, it should run after the DOM has been manipulated by Angular, but before the browser renders
if code is queued using $evalAsync from a controller, it should run before the DOM has been manipulated by Angular (and before the browser renders) -- rarely do you want this
if code is queued using $timeout, it should run after the DOM has been manipulated by Angular, and after the browser renders (which may cause flicker in some cases)

$apply

http://blog.youkuaiyun.com/dm_vincent/article/details/38705099
如果你在AngularJS上下文之外的任何地方修改了model,那么你就需要通过手动调用$apply()来通知AngularJS。这就像告诉AngularJS,你修改了一些models,希望AngularJS帮你触发watchers来做出正确的响应。

转载于:https://www.cnblogs.com/wancy86/p/5999004.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值