31

I have integrated requirejs with angular app.. before intregrating requirejs,

<input type="number" value="{{cart.quantity}}" ng-model="cart.quantity" />

was showing the value in input box.
But after integrating with requirejs, input box with type="number" not showing me the value.. input box with type="text" is working.

How can I show value with type="number" ?

Thanks

1
  • Why are you setting both value and ng-model ? ngModel will place cart.quantity into your input automatically! Commented Nov 24, 2016 at 17:24

12 Answers 12

29

I just ran into this same issue and managed to solve it. In my case, the model is being obtained via a RESTful $resource and the value for the amount is being provided as a string to the field, which in turn wipes out the value. In order to address this, I ended up doing the following in my controller:

$scope.cart = Cart.get(id: $routeParams.id, function(cart){
    cart.quantity = parseFloat(cart.quantity, 10);
});

which turns the value into a float, prior to updating the view. One gotcha I ran into is that my first attempt was setting $scope.cart.quantity = parseFloat($scope.cart.quantity, 10) immediately after the get. This was not working since the value was overwritten when the async call to get completed.

$scope.cart = Cart.get(id: $routeParams.id);
$scope.cart.quantity = parseFloat($scope.cart.quantity, 10); // This won't work

Hope this helps.

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

6 Comments

I didnt get "Expense.get(id: $routeParams.id, (e) -> e.amount = parseFloat(e.amount, 10) )"... can you pls elaborate ? or can you generate fiddle ?
It's coffeescript. I've updated the answer with Javascript, maybe this makes it clearer.
I just ran into the exact same issue binding to a '$resource` response. It's unfortunate we have to parse out the number explicitly when binding but I suppose it's a small price to pay for the validation we get for free in the view. In any event, thanks a lot for posting as this quickly helped me move past this.
+1 In my case, I've made the server-side provide an integer instead of a string. My model lives on the server.
most ridiculous bug ever
|
9

Your binded value is a string not a number.

First of all, check that your server is sending a number. If you are using PHP, you might want to use:

json_encode($array, JSON_NUMERIC_CHECK);

You might also turn your string into int or float with Javascript on the client side:

var data = ['1.9', '3']; //these won't be binded to the numbers-only input
data[0] = parseFloat(data[0]); //this will
data[1] = parseInt(data[1]);

It's not a bug as that the numbers input only accepts valid numbers (hopefully).


Note:

I also tried to bind an ng-value with an integer filter but it wont't work. Maybe because the ng-model is the one that's binded when both are found (yup, they have the same priority level).

Comments

6

I solve this problem using a custom directive:

angular.module('directives').directive('input', function() {
  return {
    restrict: 'E',
    require: 'ngModel',
    link: function(scope, $el, attrs, ngModel) {
      if ($el.get(0).type === 'number') {
        ngModel.$parsers.push(function(value) {
          return value.toString();
        });

        ngModel.$formatters.push(function(value) {
          return parseFloat(value, 10);
        });
      }
    }
  }
})

This way you do not need to change any HTTP response when the data is being obtained via a restfull resource.

Restrict this directive if necessary :)

Comments

5

value is overridden by ng-model. Remove your value property, and your ng-model will fill the input with the cart quantity.

Comments

2

I had the same problem (with an input type="range" actually) and here is my solution, using a custom directive:

app.directive('ngFloat', function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, $el, attrs, ngModel) {
        ngModel.$parsers.push(function(value) {
            return parseFloat(value, 10);
        });

        ngModel.$formatters.push(function(value) {
            return value.toString();
        });
    }
  };
});

As I restricted the directive to the "ngFloat" attribute, it is need to tag the input like this:

<input ng-float type=.........

I hope this can help future visitors.

1 Comment

I changed return value.toString(); to return (value || $el.attr('min')).toString();.. worked nicely +1
1

I added a name attribute to the input and it started working

2 Comments

Surprisingly but it worked for me. I don't understand, why. Thanks.
I can only speculate that it is related to ngForm since the ngModelController won't be available as a property of the form without a name? not seeing anything in the code (v1.5) to confirm that though.
1

If cart.quantity data type is not Number, it won't work.

Your need to

$scope.cart.quantity = parseFloat($scope.cart.quantity);

Comments

0

Your ng-model in this case cart.quantity has to be a number and not a string.

Comments

0

I found this link, which helped me. It creates a string-to-number directive, which you can attach to the number inputs in a similar fashion, that Ruy Diaz showed: https://docs.angularjs.org/error/ngModel/numfmt

Comments

0

I also ran into the same problem and tried to find a solution all the was complex for a newbie then I came across a solution at the old version of angular at https://docs.angularjs.org/api/ng/input/input%5Bnumber%5D

according to which ng-model accepts a string the workaround is that we define a

min

and

max

value while using input type=" number"

It worked for me and I hope that it work for others too

Comments

0

Strange, in my case, removing min and max attributes from the input type="number" worked for me. I mean the ng-model worked!!

Seems completely opposite to what arpit kumar posted above.

AngularJS version: 1.6.1

Comments

0
angular
  .module('xxx')
  .directive('ngFloat', function () {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function (scope, $el, attrs, ngModel) {
        ngModel.$formatters.push(function (value) {
          return +value
        });
      }
    };
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.