McNull / angular-form-gen

Design Bootstrap based form schemas for AngularJS in a drag and drop WYSIWYG environment.
MIT License
135 stars 81 forks source link

Number field not displaying data #10

Open rpapeters opened 9 years ago

rpapeters commented 9 years ago

When initially loading a form with a Number field then the value of this field is not shown, while other fields do show their data. However after editing the field, the corresponding property is updated meaning the binding should be ok.

SPeeSimon commented 7 years ago

This seems to be related to Angular 1.2. I have created a workaround with the help of http://blog.jdriven.com/2013/09/how-angularjs-directives-renders-model-value-and-parses-user-input/

angular.module('fg').directive('fgInputNumber', function() {
  return {
   require: 'ngModel',
   link: function(scope, element, attr, ctrl) {
      ctrl.$formatters.push(function(inputValue) {
          if (typeof inputValue == 'number') {
              ctrl.$setValidity('fgInputNumber', true);
              return inputValue.toString();
          }
          if( angular.isNumber(inputValue) ){
              ctrl.$setValidity('fgInputNumber', true);
          }
          return inputValue;
      });
   }
 };
});