Input model changes from Integer to String when changed

后端 未结 4 692
情深已故
情深已故 2020-12-05 06:57

Have a kind of price range/rating functionality based on an inputs model. On load, when it\'s set from the backend, it starts off as an integer, but when you type in it, it

4条回答
  •  猫巷女王i
    2020-12-05 07:23

    Very similar to the accepted answer from Yanik, except I tried that and it didn't work. This version from the AngularJS documentation is working perfectly for me, though.

    .directive('stringToNumber', function() {
        return {
            require: 'ngModel',
            link: function(scope, element, attrs, ngModel) {
              ngModel.$parsers.push(function(value) {
                return '' + value;
              });
              ngModel.$formatters.push(function(value) {
                return parseFloat(value);
              });
            }
          };
        });
    

提交回复
热议问题