一个孩子$scope
(无论是否孤立)是否可以在$scope.$apply
没有应用父范围的情况下这样做?
在父作用域中有一些昂贵的计算(很难缓存),我不需要角度来重新运行计算。
例如:
<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>
指令:
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
};
}
}
}]);