JavaScript From my directive, how can I manually trigger a digest cycle on my parent scope?

I have a directive that I would like to force render when a change is made to an underlying model programmatically. $scope.$apply is idiomatic. But I am unable to get the directive to render without clicking repeatedly on the UI (which presumably manually forces the digest).

Can someone help me identify how to manually trigger the digest?

Even this.scope.$root.$apply() has no effect.

Grabbing hold of a parent DOM node and getting the associated scope, then calling $apply on that from the console, does work however.

When using this DOM node approach in the code it does not work however. Why might this be?

Answer:1

Two things to note:

  1. Scopes inherit from each other prototypically and form a tree. Scopes have a reference to $parent which can be used to traverse.
  2. A digest cycle is something that happens on a scope object. Ie. $scope.$digest(). This runs a digest on the scope, and then recursively does the same for all of the child scopes. $scope.$apply() is like doing $rootScope.$digest(). Since it starts at the root, it calls a digest on every other scope. So $apply might be excessive.

To answer your question, scope.$parent.$digest() will trigger a digest on scope's parent.


But it seems that the triggering of the digest isn't actually your problem. $apply will trigger digests on all scopes, so if that isn't working for you, I don't think your problem is simply that you need a digest to be triggered.

Perhaps you don't need the whole directive to re-render, but just need a part of it to update? If so, consider data binding to some shared service like this.

HTML

<div ng-app='app'>
  <one></one>
  <two></two>
</div>

JS

angular
  .module('app', [])
  .directive('one', function() {
    return {
      restrict: 'E',
      scope: {},
      template: "<input ng-model='shared.val'> {{ shared.val }}",
      controller: function($scope, Shared) {
        $scope.shared = Shared;
      }
    };
  })
  .directive('two', function() {
    return {
      restrict: 'E',
      scope: {},
      template: '<p> {{ shared.val }}</p>',
      controller: function($scope, Shared) {
        $scope.shared = Shared;
      }
    };
  })
  .factory('Shared', function() {
    return {
      val: ''
    };
  });
Answer:2

I have many divs with id="imgLinks" <div><img id="imgLinks" u=image src="../../../../images/1.jpg" /></div> <div><img id="imgLinks" u=image src="../../../../images/3.jpg"...

I have many divs with id="imgLinks" <div><img id="imgLinks" u=image src="../../../../images/1.jpg" /></div> <div><img id="imgLinks" u=image src="../../../../images/3.jpg"...

I saw this pattern being used in some code... currency = $(document.getElementById('currency')); Which is expect is functionally equivalent to... currency = $('#currency'); I am guessing the idea ...

I saw this pattern being used in some code... currency = $(document.getElementById('currency')); Which is expect is functionally equivalent to... currency = $('#currency'); I am guessing the idea ...

  1. document.getelementbyid jquery
  2. document.getelementbyid jquery alternative
  3. document.getelementbyid jquery equivalent
  4. document.getelementbyid jquery selector
  5. document.getelementbyid jquery value
  6. document.getelementbyid jquery example
  7. document.getelementbyid vs jquery
  8. document.getelementbyid vs jquery performance
  9. document.getelementbyid innerhtml jquery
  10. document.getelementbyid vs jquery selector
  11. document.getelementbyid in jquery w3schools
  12. document.getelementbyid using jquery
  13. document.getelementbyid.src jquery
  14. document.getelementbyid(id) jquery
  15. document.getelementbyid or jquery faster
  16. parent.document.getelementbyid jquery
  17. document.getelementbyid vs jquery id selector
  18. javascript document.getelementbyid jquery
  19. document getelementbyid disabled jquery
  20. document.getelementbyid.checked jquery

I want to get the selected node path from root in tree view by using AngularJS. I have seen this link. It is used to get only selected node like subUser2. But i want to display selected Node path or ...

I want to get the selected node path from root in tree view by using AngularJS. I have seen this link. It is used to get only selected node like subUser2. But i want to display selected Node path or ...

I am developing a new library above amqp.node (amqplib), basically we dont need all the RabbitMQ functionality. So, I am creating a simple library that facilitates the usage specifically for our ...

I am developing a new library above amqp.node (amqplib), basically we dont need all the RabbitMQ functionality. So, I am creating a simple library that facilitates the usage specifically for our ...

  1. nodejs best practices github
  2. node js best practices 2019
  3. node js best practices file structure
  4. node js best practices 2020
  5. node js best practices medium
  6. node js best practices folder structure
  7. node js best practices for production
  8. node js best practices require
  9. node js best practices
  10. node js best practices 2018
  11. node js module best practices
  12. node js best practices tutorial
  13. mongoose node js best practices
  14. node js best practices conventions
  15. nodejs logging best practices
  16. nodejs docker best practices
  17. nodejs express best practices
  18. nodejs api best practices
  19. nodejs dockerfile best practices
  20. nodejs rabbitmq best practices