Cancel $digest

91 views
Skip to first unread message

jmeco

unread,
Dec 20, 2012, 6:25:51 AM12/20/12
to ang...@googlegroups.com
Hi,

Is there a way to cancel the running digest. 
In my app I have a ngClick handler on a text which should set the value of my search input, which in turn is used to filter a list of documents. When the value of the text is equal to the value already in the search input, I don't set the search input value and I don't call my filter function (filtering is performed in a service instead of the view with the "|" syntax because it used in several places).

I avoid doing intensive operations, but the $digest still happen and angularjs run all the watches. Since nothing changed (value clicked === search input) I would like to tell to angular to cancel the current digest. Is there a way to do this ?


Example
In the template I have:

<span  data-ng-click="replaceQuery(tr.categoryName, $event)">{{tr.categoryName}}</span>

In the controller I have:

  $scope.replaceQuery = function (searchName, $event) {
                if ($event) {
                    $event.stopPropagation();
                }
                if (TransactionsList.query !== searchName) {
                    TransactionsList.query =  searchName;
                    TransactionsList.applyFilters();
                    $scope.$digest();
                }
      };


Peter Bacon Darwin

unread,
Dec 20, 2012, 6:35:06 AM12/20/12
to ang...@googlegroups.com
Cancelling a digest in the middle would be bad since you could be left with scopes that are not accurate.
In the code above you should not be calling $digest in your replaceQuery function since it is being called by the ng-click directive which already does the digest so you should be getting an error ($digest already running!).
If nothing has changed when the click event occurs then the digest will run through all the watches exactly once and not call any handlers since nothing has changed.  Unless you are doing something very naughty in your watch functions then this should be extremely quick.  So there should be no need to worry about cancelling the digest.
Pete




--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

jmeco

unread,
Dec 20, 2012, 10:49:58 AM12/20/12
to ang...@googlegroups.com
It did a bad copy/paste in the example, it should read :

$scope.replaceQuery = function (searchName, $event) {
                if ($event) {
                    $event.stopPropagation();
                }
                if (TransactionsList.query !== searchName) {
                    TransactionsList.query =  searchName;
                    TransactionsList.applyFilters();
                }
      };

The things is that the $digest is not started when my function execute, it will be triggered by the $apply of the ngClick directive after my function return, so it should be safe to not start it if there were a method for that. 
I tried to used onclick and call $digest (further optimisation is that I need to run the $digest in case of changes only in the scopes subtree, so $scope.$digest instead of $roor.$digest is ok for my case) :

<span  onclick="{{replaceQuery(tr.categoryName, $event)}}">{{tr.categoryName}}</span>

In the controller I have:

  $scope.replaceQuery = function (searchName, $event) {
                if ($event) {
                    $event.stopPropagation();
                }
                if (TransactionsList.query !== searchName) {
                    TransactionsList.query =  searchName;
                    TransactionsList.applyFilters();
                    $scope.$digest();
                }
      };

But it does not work because the onclick expression is evaluated during a $digest so the call to $digest failed.

Anyway, maybe it is unecessary optimization as you suggest.

Thanks Peter for the answer,
Reply all
Reply to author
Forward
0 new messages