$parsers first understand its specific role, when the user interacts with the controller. The $setviewvalue () method in the Ngmodelcontroller is called, and the function in the $parsers array is called by one by one in the form of pipelining. The first $parse is called after a second $parse call is madeThese functions can convert the input values, which set the legitimacy of the expression through the $setvalidity () function:This method can be used to artificially set the $valid and $ invalid of a form control, that is, to change whether the form control passes the state of the checksum. Similar to $setdirty () and $setpristine (). So specific is to achieve, with some time to do a surprise test, also did not spend any time to study, there is inappropriate and normal, but it should be almost the first $parsers is a view to the model of an excessive, so when the model does not want to update the time to return a undefined. In fact, the initial look at it is to change the value of the Ngmodel, but the test will find that you enter input is not empty, or you enter the number, but you will find that if it is triggered, the model inside the property will not be updated, So when the model doesn't want to be updated, it returns a undefined. There is no problem with this sentence. It is only model not updated, but your view will be updated, because the content we entered is not in the Ng-model, but he did not connect with the model, well, my understanding is to lose the two-way binding characteristics, closed "scope". This service is used more than one instruction, so the test finds that you have to add him if you want to use the command, even if you feel there is no place to call it, but it does exist there. If you take the following example ng-test to see and then add the attribute not to call in the look, as well as I write a ng-test look.So when is the model turn view is another built-in service $formatters, then this is actually $parsers execution immediately after the call service. That's not much to say.。
//html<div ng-controller= "TestController" ><form name= "Testform" ><input type= "text" name= "inputs" ng-test ng-model= "Obj.number" ><span ng-show= "testform.inputs. $error. Test" >good</span><span ng-hide= "testform.inputs. $error. Test" >bad</span><div>{{testform.inputs. $valid}}</div><div>{{testform.inputs. $invalid}}</div> <div>{{obj.number}}</div></form></div>
//script<script type= "Text/javascript" >angular.module (' myApp ', []). Controller (' TestController ', function ($scope) {$scope. obj = {number:34 }; }) . directive (' Ngtest ', function () {return {require: '? Ngmodel ',restrict: ' AE ',link:function ($scope, IELM, Iattrs, Ngmodel) {if (!ngmodel) return;Ngmodel. $parsers. Push (function (viewvalue) {var num = parseint (viewvalue);if (num>=0 && num<99) {Ngmodel. $setValidity (' Test ', true);return viewvalue;}else{Ngmodel. $setValidity (' test ', false);return undefined; } }); } }; }); </script>
angularjs-$parsers Self-understanding-analysis