@@ -2,18 +2,50 @@ part of angular.core;
2
2
3
3
4
4
/**
5
- * Used by [Scope.$on] to notify the listeners of events.
5
+ * Injected into the listener function within [Scope.$on] to provide event-specific
6
+ * details to the scope listener.
6
7
*/
7
8
class ScopeEvent {
9
+
10
+ /**
11
+ * The name of the intercepted scope event.
12
+ */
8
13
String name;
14
+
15
+ /**
16
+ * The origin scope that triggered the event (via $broadcast or $emit).
17
+ */
9
18
Scope targetScope;
19
+
20
+ /**
21
+ * The destination scope that intercepted the event.
22
+ */
10
23
Scope currentScope;
24
+
25
+ /**
26
+ * true or false depending on if stopPropagation() was executed.
27
+ */
11
28
bool propagationStopped = false ;
29
+
30
+ /**
31
+ * true or false depending on if preventDefault() was executed.
32
+ */
12
33
bool defaultPrevented = false ;
13
34
35
+ /**
36
+ ** [name] - The name of the scope event.
37
+ ** [targetScope] - The destination scope that is listening on the event.
38
+ */
14
39
ScopeEvent (this .name, this .targetScope);
15
40
41
+ /**
42
+ * Prevents the intercepted event from propagating further to successive scopes.
43
+ */
16
44
stopPropagation () => propagationStopped = true ;
45
+
46
+ /**
47
+ * Sets the defaultPrevented flag to true.
48
+ */
17
49
preventDefault () => defaultPrevented = true ;
18
50
}
19
51
@@ -50,9 +82,20 @@ class Scope implements Map {
50
82
final bool _isolate;
51
83
final bool _lazy;
52
84
final Profiler _perf;
85
+
86
+ /**
87
+ * The direct parent scope that created this scope (this can also be the $rootScope)
88
+ */
53
89
final Scope $parent;
54
90
91
+ /**
92
+ * The auto-incremented ID of the scope
93
+ */
55
94
String $id;
95
+
96
+ /**
97
+ * The topmost scope of the application (same as $rootScope).
98
+ */
56
99
Scope $root;
57
100
num _nextId = 0 ;
58
101
String _phase;
@@ -409,6 +452,21 @@ class Scope implements Map {
409
452
this ._disabled = false ;
410
453
}
411
454
455
+ /**
456
+ * Processes all of the watchers of the current scope and its children.
457
+ * Because a watcher's listener can change the model, the `$digest()` operation keeps calling
458
+ * the watchers no further response data has changed. This means that it is possible to get
459
+ * into an infinite loop. This function will throw `'Maximum iteration limit exceeded.'`
460
+ * if the number of iterations exceeds 10.
461
+ *
462
+ * There should really be no need to call $digest() in production code since everything is
463
+ * handled behind the scenes with zones and object mutation events. However, in testing
464
+ * both $digest and [$apply] are useful to control state and simulate the scope life cycle in
465
+ * a step-by-step manner.
466
+ *
467
+ * Refer to [$watch] , [$watchSet] or [$watchCollection] to see how to register watchers that
468
+ * are executed during the digest cycle.
469
+ */
412
470
$digest () {
413
471
try {
414
472
_beginPhase ('\$ digest' );
@@ -600,6 +658,21 @@ class Scope implements Map {
600
658
}
601
659
602
660
661
+ /**
662
+ * Removes the current scope (and all of its children) from the parent scope. Removal implies
663
+ * that calls to $digest() will no longer propagate to the current scope and its children.
664
+ * Removal also implies that the current scope is eligible for garbage collection.
665
+ *
666
+ * The `$destroy()` operation is usually used within directives that perform transclusion on
667
+ * multiple child elements (like ngRepeat) which create multiple child scopes.
668
+ *
669
+ * Just before a scope is destroyed, a `$destroy` event is broadcasted on this scope. This is
670
+ * a great way for child scopes (such as shared directives or controllers) to detect to and
671
+ * perform any necessary cleanup before the scope is removed from the application.
672
+ *
673
+ * Note that, in AngularDart, there is also a `$destroy` jQuery DOM event, which can be used to
674
+ * clean up DOM bindings before an element is removed from the DOM.
675
+ */
603
676
$destroy () {
604
677
if ($root == this ) return ; // we can't remove the root node;
605
678
@@ -612,11 +685,32 @@ class Scope implements Map {
612
685
}
613
686
614
687
688
+ /**
689
+ * Evaluates the expression against the current scope and returns the result. Note that, the
690
+ * expression data is relative to the data within the scope. Therefore an expression such as
691
+ * `a + b` will deference variables `a` and `b` and return a result so long as `a` and `b`
692
+ * exist on the scope.
693
+ *
694
+ * * [expr] - The expression that will be evaluated. This can be both a Function or a String.
695
+ * * [locals] - An optional Map of key/value data that will override any matching scope members
696
+ * for the purposes of the evaluation.
697
+ */
615
698
$eval (expr, [locals]) {
616
699
return relaxFnArgs (_compileToFn (expr))(locals == null ? this : new ScopeLocals (this , locals));
617
700
}
618
701
619
702
703
+ /**
704
+ * Evaluates the expression against the current scope at a later point in time. The $evalAsync
705
+ * operation may not get run right away (depending if an existing digest cycle is going on) and
706
+ * may therefore be issued later on (by a follow-up digest cycle). Note that at least one digest
707
+ * cycle will be performed after the expression is evaluated. However, If triggering an additional
708
+ * digest cycle is not desired then this can be avoided by placing `{outsideDigest: true}` as
709
+ * the 2nd parameter to the function.
710
+ *
711
+ * * [expr] - The expression that will be evaluated. This can be both a Function or a String.
712
+ * * [outsideDigest] - Whether or not to trigger a follow-up digest after evaluation.
713
+ */
620
714
$evalAsync (expr, {outsideDigest: false }) {
621
715
if (outsideDigest) {
622
716
_outerAsyncQueue.add (expr);
@@ -651,6 +745,16 @@ class Scope implements Map {
651
745
}
652
746
653
747
748
+ /**
749
+ * Triggers a digest operation much like [$digest] does, however, also accepts an
750
+ * optional expression to evaluate alongside the digest operation. The result of that
751
+ * expression will be returned afterwards. Much like with $digest, $apply should only be
752
+ * used within unit tests to simulate the life cycle of a scope. See [$digest] to learn
753
+ * more.
754
+ *
755
+ * * [expr] - optional expression which will be evaluated after the digest is performed. See [$eval]
756
+ * to learn more about expressions.
757
+ */
654
758
$apply ([expr]) {
655
759
return _zone.run (() {
656
760
var timerId;
@@ -666,6 +770,23 @@ class Scope implements Map {
666
770
}
667
771
668
772
773
+ /**
774
+ * Registers a scope-based event listener to intercept events triggered by
775
+ * [$broadcast] (from any parent scopes) or [$emit] (from child scopes) that
776
+ * match the given event name. $on accepts two arguments:
777
+ *
778
+ * * [name] - Refers to the event name that the scope will listen on.
779
+ * * [listener] - Refers to the callback function which is executed when the event
780
+ * is intercepted.
781
+ *
782
+ *
783
+ * When the listener function is executed, an instance of [ScopeEvent] will be passed
784
+ * as the first parameter to the function.
785
+ *
786
+ * Any additional parameters available within the listener callback function are those that
787
+ * are set by the $broadcast or $emit scope methods (which are set by the origin scope which
788
+ * is the scope that first triggered the scope event).
789
+ */
669
790
$on (name, listener) {
670
791
var namedListeners = _listeners[name];
671
792
if (! _listeners.containsKey (name)) {
@@ -679,6 +800,16 @@ class Scope implements Map {
679
800
}
680
801
681
802
803
+ /**
804
+ * Triggers a scope event referenced by the [name] parameters upwards towards the root of the
805
+ * scope tree. If intercepted, by a parent scope containing a matching scope event listener
806
+ * (which is registered via the [$on] scope method), then the event listener callback function
807
+ * will be executed.
808
+ *
809
+ * * [name] - The scope event name that will be triggered.
810
+ * * [args] - An optional list of arguments that will be fed into the listener callback function
811
+ * for any event listeners that are registered via [$on] .
812
+ */
682
813
$emit (name, [List args]) {
683
814
var empty = [],
684
815
namedListeners,
@@ -713,6 +844,16 @@ class Scope implements Map {
713
844
}
714
845
715
846
847
+ /**
848
+ * Triggers a scope event referenced by the [name] parameters dowards towards the leaf nodes of the
849
+ * scope tree. If intercepted, by a child scope containing a matching scope event listener
850
+ * (which is registered via the [$on] scope method), then the event listener callback function
851
+ * will be executed.
852
+ *
853
+ * * [name] - The scope event name that will be triggered.
854
+ * * [listenerArgs] - An optional list of arguments that will be fed into the listener callback function
855
+ * for any event listeners that are registered via [$on] .
856
+ */
716
857
$broadcast (String name, [List listenerArgs]) {
717
858
var target = this ,
718
859
current = target,
0 commit comments