0

I am trying to bind a value which of type string when it comes from the controller in a numeric input field like the following:

<input type="number" ng-model="detail.Value">

Since the detail.Value is of type string, I value is not getting displayed (I guess).

I can not change the Value property's type to int.

What can I do in the view to display the value in the number box?

4
  • are these data comes from db ? Commented Sep 29, 2014 at 11:46
  • Yes kind of, I am saving them into a $scope variable and displaying them @KalhanoToressPamuditha I am getting the values as JSON object and assigning them.. to be precise I am returning a List of object Commented Sep 29, 2014 at 11:46
  • i also had this problem. what i did is change the db column data type to integer or double or some numeric type. Commented Sep 29, 2014 at 12:02
  • @KalhanoToressPamuditha in my case I can't do that that's the sad part Commented Sep 29, 2014 at 12:03

4 Answers 4

1

Wrap it with parseFloat:

 detail.Value = parseFloat(detail.Value);

Demo Fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

@RandomUser do you mean in HTML?
@RandomUser the answer is NO, you can't use ng-model with function. But i suggest you to make it anyways in controller. Not big dial.
@mMaximShoustin in the fiddle I am not able to see 123 in the number box
well, it should work in Chrome, firefox, IE and Safari
1
$scope.isNumeric = function isNumber(number) {               // function to test the data can be convert to numeric
     return !isNaN(parseFloat(number)) && isFinite(number);
}


...success(data) {      // after getting the data from serverside
    var formData = data.formDataWithNumeric; // get the form data
    for (key in formData) {                  //iterate through the data
        var isNumericData = $scope.isNumeric(formData[key]);  // check data can be convert to a numeric
        if(isNumericData) {
            formData[key] = Number(formData[key]);  // reassign the numeric value instead of string value
        }
    }
}

you can try something like this

Comments

1

You can either to pass that value to the method on a controller which will do the parsing (and assigning to the model) or if you really want to do that in HTML you can assign parsing method to the scope in the controller ($scope.parseFloat = parseFloat;) and than parse in ng-init. Something like this: ng-init="value = parseFloat(detail.Value)" and then ng-model="value".

Comments

1

Here is a small directive to force the model value to be a number

   function forceNumber ($parse) {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, elem, attrs, ctrl) {
           var getter = $parse(attrs.ngModel);
           var setter = getter.assign;
           var remListner = attrs.$observe('ngModel', function (val) {
              var value = getter(scope);
              if (typeof value !== 'number') {
                 setter(scope,+value);
              }
              // remove listner, no need to keep watching!
              remListner();
           });
        }
    };
   }

you can see it in action in this small plunker

Hope this helps you, Regards Sander

1 Comment

Sorry, forgot to save the plunk :) done now, and updated my answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.