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?
Two things to note:
$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: ''
};
});
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 ...
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 ...