@@ -178,6 +178,7 @@ function $RootScopeProvider() {
178
178
this . $$childHead = this . $$childTail = null ;
179
179
this . $root = this ;
180
180
this . $$destroyed = false ;
181
+ this . $$suspended = false ;
181
182
this . $$listeners = { } ;
182
183
this . $$listenerCount = { } ;
183
184
this . $$watchersCount = 0 ;
@@ -811,7 +812,7 @@ function $RootScopeProvider() {
811
812
812
813
traverseScopesLoop:
813
814
do { // "traverse the scopes" loop
814
- if ( ( watchers = current . $$watchers ) ) {
815
+ if ( ( watchers = ! current . $$suspended && current . $$watchers ) ) {
815
816
// process our watches
816
817
watchers . $$digestWatchIndex = watchers . length ;
817
818
while ( watchers . $$digestWatchIndex -- ) {
@@ -855,7 +856,7 @@ function $RootScopeProvider() {
855
856
// Insanity Warning: scope depth-first traversal
856
857
// yes, this code is a bit crazy, but it works and we have tests to prove it!
857
858
// this piece should be kept in sync with the traversal in $broadcast
858
- if ( ! ( next = ( ( current . $$watchersCount && current . $$childHead ) ||
859
+ if ( ! ( next = ( ( ! current . $$suspended && current . $$watchersCount && current . $$childHead ) ||
859
860
( current !== target && current . $$nextSibling ) ) ) ) {
860
861
while ( current !== target && ! ( next = current . $$nextSibling ) ) {
861
862
current = current . $parent ;
@@ -892,6 +893,58 @@ function $RootScopeProvider() {
892
893
$browser . $$checkUrlChange ( ) ;
893
894
} ,
894
895
896
+ /**
897
+ * @ngdoc method
898
+ * @name $rootScope.Scope#$suspend
899
+ * @kind function
900
+ *
901
+ * @description
902
+ * Suspend watchers of this scope subtree so that they will not be invoked during digest.
903
+ *
904
+ * This can be used to optimize your application when you know that running those watchers
905
+ * is redundant.
906
+ *
907
+ * **Warning**
908
+ *
909
+ * Suspending scopes from the digest cycle can have unwanted and difficult to debug results.
910
+ * Only use this approach if you are confident that you know what you are doing and have
911
+ * ample tests to ensure that bindings get updated as you expect.
912
+ *
913
+ * Some of the things to consider are:
914
+ *
915
+ * * Any external event on a directive/component will not trigger a digest while the hosting
916
+ * scope is suspended - even if the event handler calls `$apply` or `$digest`.
917
+ * * Transcluded content exists on a scope that inherits from outside a directive but exists
918
+ * as a child of the directive's containing scope. If the containing scope is suspended the
919
+ * transcluded scope will also be suspended, even if the scope from which the transcluded
920
+ * scope inherits is not suspended.
921
+ * * Multiple directives trying to manage the suspended status of a scope can confuse each other:
922
+ * * A call to `$suspend` an already suspended scope is a no-op.
923
+ * * A call to `$resume` a non-suspended scope is a no-op.
924
+ * * If two directives suspend a scope, then one of them resumes the scope, the scope will no
925
+ * longer be suspended. This could result in the other directive believing a scope to be
926
+ * suspended when it is not.
927
+ */
928
+ $suspend : function ( ) {
929
+ this . $$suspended = true ;
930
+ } ,
931
+
932
+ /**
933
+ * @ngdoc method
934
+ * @name $rootScope.Scope#$resume
935
+ * @kind function
936
+ *
937
+ * @description
938
+ * Resume watchers of this scope subtree in case it was suspended.
939
+ *
940
+ * It is recommended to digest on this scope after it is resumed to catch any modifications
941
+ * that might have happened while it was suspended.
942
+ *
943
+ * See {@link $rootScope.Scope.$suspend} for information about the dangers of using this approach.
944
+ */
945
+ $resume : function ( ) {
946
+ this . $$suspended = false ;
947
+ } ,
895
948
896
949
/**
897
950
* @ngdoc event
0 commit comments