什么是"范围"获取传递到$范围。$观看功能? [英] What is the "scope" that gets passed to a $scope.$watch function?

查看:215
本文介绍了什么是"范围"获取传递到$范围。$观看功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$scope.$watch(
    function(scope) {return scope.aNumber;},
    function(newValue, oldValue) {alert("Value changed");}
    );
});

什么是范围的$范围。$腕表发生在它的第一个功能? (在此之后一切都是额外的信息是切向相关)。我知道,范围没有$一般重新present只是一个变量,该指令链接功能(范围,元素,属性,ngController)等,但我不知道它在哪儿来自在这里。显然,它连接到控制器的$范围,但如何?

What is the "scope" that $scope.$watch takes in its first function? (everything after this is extra info that is tangentially related). I know "scope" without the $ generally represent just a variable, as in the directive link function (scope, element, attributes, ngController), etc. However I have no idea where it "comes from" here. Clearly it's connected to the controller's $scope, but how?

此外,官方文档指出watchEx pression的)呼吁在每次调用$消化(),应该返回将要观看的价值。(因为$摘要(重播检测时改变了watchEx pression可以每$摘要多次执行(),应幂等)。所以,有什么优势,在返回的功能,而不是只说$ scope.valueToWatch(它不适合我,但我所看到的人做到这一点)。

Also, the official doc states "The watchExpression is called on every call to $digest() and should return the value that will be watched. (Since $digest() reruns when it detects changes the watchExpression can execute multiple times per $digest() and should be idempotent.)" So what's the advantage to doing the return function rather than just saying $scope.valueToWatch (which doesn't work for me, but I have seen people do it).

普拉克有工作表只是为了它的地狱,并不真正需要它的问题:
http://plnkr.co/edit/y86Wr93xLIao3wTwVsT8?p=$p$pview

Plunk with working watch just for the hell of it, don't really need it for q: http://plnkr.co/edit/y86Wr93xLIao3wTwVsT8?p=preview

对于那些有相同的q读书迟:
在$表的好文章:
http://tutorials.jenkov.com/angularjs/watch -digest-apply.html

For those reading with same q later: Good article on $watch: http://tutorials.jenkov.com/angularjs/watch-digest-apply.html

推荐答案

只需将相同从中 $腕表正在执行范围。使用这一个,而不是 $范围唯一的好处是,它避免了你一个没用的关闭,但它确实是一样的。

Simply the same scope from which the $watch is being executed. The only advantage to using this one instead of $scope is that it avoids you a useless closure, but it does the same.

我不知道理解你的第二个问题,但是请注意,这是等价的:

I'm not sure to understand your second question, but note that these are equivalent:

1. $scope.$watch(function(scope) { return scope.prop1.prop2; }, cb);
2. $scope.$watch(function() { return $scope.prop1.prop2; }, cb);
3. $scope.$watch('prop1.prop2', cb);

这篇关于什么是"范围"获取传递到$范围。$观看功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆