Is it possible for a child $scope
(isolated or not) to do $scope.$apply
without the parrent scope being applied?
There is some expensive computation going on (that is hard to cache) in the parent scope and I don't need angular to rerun the computation again.
for instance:
<div ng-controller="ParentController">
{{ expensiveFunction() }}
<div directive>
<h1 ng-click="applyChildScopeOnly()">Click {{ value }}</h1>
</div>
<div directive>
<h1 ng-click="applyChildScopeOnly()">Click {{ value }}</h1>
</div>
<button ng-click="applyChildrenScope()"/> <!-- apply to children scope only -->
</div>
The directive:
module.directive('directive', ['$document','$rootScope', function ($document,$rootScope) {
return{
restrict:'AE',
scope:{},
link:function($scope, element, attrs){
$scope.applyChildScopeOnly = function(){
$scope.$apply(); // don't apply changes to $parent scope
};
}
}
}]);
You can call $scope.$digest()
instead of $scope.$apply()
to re-evaluate watches in the current scope and all its children. Calling $scope.$digest()
wont't evaluate any watches on any parent scopes.
As a side note, the $scope.$apply()
calls the $rootScope.$digest()
behind the scenes.
More info here: http://docs.angularjs.org/api/ng.$rootScope.Scope
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With