[AngularJS] $.apply() vs. $.digest()
digest only work on the current scope, has no affect on the parent scope!
apply works for all the parents scope and current scope!
So, if you click digest button, only the brown color will change color.
If you click the apply button, all the borders will change color.
In summary, only if when $.apply() method will invoke some filter which does heavy stuff or will only want current scope get effected, then you might want to use digest. Otherwise, keep away from digest, it should be fine to use apply.
angular.module("demo", []) .filter("heavy", function () { return function (val) { return val + " heavy!!! " + moment().format('SSS'); } }) .controller("OuterCtrl", function ($scope, $element) { var outer = this; $scope.$watch(function () { TweenMax.to($element, .5, {borderColor: tinycolor.fromRatio({ r: Math.random(), g: Math.random(), b: Math.random() }).toHexString()}) }) }) .controller("MiddleCtrl", function ($scope, $element) { var middle = this; $scope.$watch(function () { TweenMax.to($element, .5, {borderColor: tinycolor.fromRatio({ r: Math.random(), g: Math.random(), b: Math.random() }).toHexString()}) }) }) .controller("InnerCtrl", function ($scope, $element) { var inner = this; $scope.$watch(function () { TweenMax.to($element, .5, {borderColor: tinycolor.fromRatio({ r: Math.random(), g: Math.random(), b: Math.random() }).toHexString()}) }) }) .directive("digestButton", function ($rootScope) { return { restrict: "E", template: "<button class='btn'>$digest {{digestCount}}</button>", link: function (scope, element, attrs, ctrl) { scope.digestCount = 0; element.on("click", function () { $rootScope.time = moment().format('h:mm:ss'); scope.digestCount++; scope.$digest(); }) } } }) .directive("applyButton", function ($rootScope) { return { restrict: "E", template: "<button class='btn'>$apply {{applyCount}}</button>", link: function (scope, element, attrs, ctrl) { scope.applyCount = 0; element.on("click", function () { $rootScope.time = moment().format('h:mm:ss'); scope.applyCount++; scope.$apply(); }) } } })
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css"/> <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/tinycolor/0.11.1/tinycolor.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/gsap/1.12.1/TweenMax.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.js"></script> <script src="app.js"></script> <style> .well{ border: 30px solid black; } </style> </head> <body ng-app="demo" class="container"> <div class="well" ng-controller="OuterCtrl as outer"> <h2>Outer {{ time | heavy}}</h2> <div class="well" ng-controller="MiddleCtrl as middle"> <h2>Middle {{ time }}</h2> <div class="well" ng-controller="InnerCtrl as inner"> <h2>Inner {{ time }}</h2> <digest-button></digest-button> <apply-button></apply-button> </div> </div> </div> </body> </html>
Read More: https://egghead.io/lessons/angularjs-apply-vs-digest
分类:
AngularJS
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具