javascript - Why would a hash computation using CryptoJS cause a $rootScope:infdig error in Angular? -
I have a simple page that shows the hash of a string because someone has typed it in the page. I have found that the page has a JavaScript error
Error: [$ rootScope: infdig] $ rootScope / infdig? P0 = 10 & amp; P1 =% 5B% 5B% 22sha1 ... 75651% 2C1080464653% 2C-772792499% 5 %% 2C% 5C% 22 SGbits% 5C% 22% 3A20% 7D% 22% 5D% 5D
There is a very simple version of the page
& lt; Html lang = "n" & gt; & Lt; Top & gt; & Lt; Script src = "http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js" & gt; & Lt; / Script & gt; & Lt; Script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js" & gt; & Lt; / Script & gt; & Lt; Script & gt; Function Microcution ($ scope) {$ scope.sha1 = function (pwd) {return cryptoos SHA1 (pwd); }; } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div class = "app" ng-ap ng-controller = "myctrl" & gt; & Lt; Span ng-bind = "sha1 ('bar')" & gt; & Lt; / Span & gt; & Lt; / Div & gt; & Lt; / Body & gt; & Lt; / Html & gt; which is available as a planner.
What I'm trying to do in the original page, the hash looks like this in a form field and the re-definition in the input field
and ng-bind is actually ng-bind = "sha1 (password)" , but the simple static case in Plunker shows the same behavior.
I have collected that infinite error is to do with a lot of $ Digest Chakras, but I do not know how it will be. It seems that hash triggers an error in the filing error, because there is no error by returning a stable string from the sha1 function.
ng-bind = "sha1 ('bar')" The Digest makes the cycle unstable, each time the sha1 function returns a different object (the reference is different) and to stabilize your digest cycle to run again this and every digestive cycle again evaluates the expression of the ng-bine function And until it determines the maximum ceiling (10) does not last long. You can easily repeat this problem by doing just return [] in your scope method, it's just such a good habit to force a work expression for ng-bind There is a side effect because it runs every digest cycle, if all of it should be carefully evaluated.
A simple solution is to tie a ng-change / ng-blur event to your password or any other trigger and force the NIG-bind to the property instead of the expression.
angular.module ('app', []). Constant ('crypto', window cryptoses); Function Michael ($ scope, crypto) {$ scope.encrypt = function () {$ scope.encrypted = Crypto.SHA1 ($ scope.password); }; } & lt; Html lang = "en" ng-app = "app" & gt; & Lt; Top & gt; & Lt; Script src = "http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js" & gt; & Lt; / Script & gt; & Lt; Script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js" & gt; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div class = "app" ng-ap ng-controller = "myctrl" & gt; & Lt; Input id = "password" ng-model = "password" type = "password" placeholder = "password" ng-change = "encrypt ()"> & Lt; Span ng-bind = "encrypted" & gt; & Lt; / Span & gt; & Lt; / Div & gt; & Lt; / Body & gt;
Comments
Post a Comment