Recently I was implementing a small workaround solution for angular.js date input control and discovered useful $parsers/$formatters pipeline of NgModelController (controller of ng-model directive).
Many HTML element directives may interact with ngModel, each putting parser handlers to the beginning of parsing pipeline and formatter handlers to the end of formatting pipeline. These pipelines transform values between ngModel.$viewValue (what you see) and ngModel.$modelValue (what you get in your model). Using directive priority setting, one can ensure that $parsers and $formatters are in the correct order.
In the following example, ngModel is an instance of ng-model directive controller. We get a single instance of it per DOM element.
Many HTML element directives may interact with ngModel, each putting parser handlers to the beginning of parsing pipeline and formatter handlers to the end of formatting pipeline. These pipelines transform values between ngModel.$viewValue (what you see) and ngModel.$modelValue (what you get in your model). Using directive priority setting, one can ensure that $parsers and $formatters are in the correct order.
The problem with the control was that it did not support manual date entry. We were using old version of angular.js, without ng-model-options support, so on every keystroke, this control tried to convert input string value to date object. This obviously failed in the middle of entry and input was being cleared.
So easy workaround in our case was to create new directive with higher priority setting and put a function in the beginning of parsing pipeline. This function would simply transform input value into necessary state for the input control to work correctly.
Obviously, these pipelines are not for workarounds only. They may be very useful, when implementing custom controls and combining them with any 3rd party solutions.
So easy workaround in our case was to create new directive with higher priority setting and put a function in the beginning of parsing pipeline. This function would simply transform input value into necessary state for the input control to work correctly.
Obviously, these pipelines are not for workarounds only. They may be very useful, when implementing custom controls and combining them with any 3rd party solutions.
Comments