-
Notifications
You must be signed in to change notification settings - Fork 27.4k
AngularJS version 1.3.13 throws an error Error: [ngModel:numfmt] Expected 0.00
to be a number
#11157
Comments
If I remove the last line for the custom directive smNumber, I don't get that error. |
Could you please provide a working example of your code using plunkr? http://plnkr.co/ |
It's a bit tricky as this is part of a big complex project, but I'll try later on. I've traced the code and the value was coming as "0.00" (e.g. string) and the first check for not empty was returning false and then the second check for isNumber was returning false as well (perhaps because the value was passed as a string). |
Hi Matias, The failing code is this: ctrl.$formatters.push(function(value) { The isNumber function checks the typeof value and not if that value is a In our case we use the following code in HTML:
@Labels.percentage:
ng-model="currentSpecial.discPct" type="number" data-sm:number data-sm:number-format data-accuracy="0"> % @String.Format(Messages.mustBeBetweenXandY, Labels.percentage, "-100", and I attach the smNumber and smNumberFormat directives to this email. I've What would you suggest for the fix here? Thanks in advance. On Tue, Feb 24, 2015 at 12:31 AM, Matias Niemelä [email protected]
|
So this is the current angularjs implementation of the number format: function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) { ctrl.$$parserName = 'number'; ctrl.$formatters.push(function(value) { if (attr.min || attr.ngMin) {
validations if (attr.max || attr.ngMax) {
validations I am guessing something has changed since the version 1.10.2 we were using So, what is the best course of actions for us to be able to format the Thanks in advance. I am thinking I may try to add lower priority to our formatting directive UPDATE. Adding priority: - 3 to the top of our smNumberFormat directive didn't change the outcome, I still get the same error. On Tue, Feb 24, 2015 at 1:39 PM, Naomi Nosonovsky [email protected]
|
Can you please put a number input with your custom directives into a plnkr? The framework code is not particularly helpful, I'm afraid. |
I am having same issue with an customized angular touchspin directive (https://gist.github.com/jdelibas/b20160fced5963a338ab#file-spindirective-js) of jquery's touchspin plugin (http://www.virtuosoft.eu/code/bootstrap-touchspin): 'use strict';
angular.module('app')
.directive('spin', function() {
return {
templateUrl: 'components/templates/touchspin.html',
scope: {
bindModel:'=ngModel',
change :'&ngChange'
},
restrict: 'E',
require: 'ngModel',
bindAttr: '=',
link: function(scope, element, attrs) {
var min,max,step,input,initial, decimals, prefix, verticalbuttons, stepinterval;
element = angular.element(element);
if(typeof attrs === 'undefined')throw new Error('Spin.js attributes missing');
min = typeof attrs.min !== 'undefined' ? parseFloat(attrs.min) : 1;
max = typeof attrs.max !== 'undefined' ? parseFloat(attrs.max) : 999;
step = typeof attrs.step !== 'undefined' ? parseFloat(attrs.step) : 1;
prefix = typeof attrs.prefix !== 'undefined' ? attrs.prefix : '';
decimals = typeof attrs.decimals !== 'undefined' ? parseInt(attrs.decimals) : 0;
input = $('input[name="spin"]',element);
input.TouchSpin({
min: min,
max: max,
step: step,
decimals: decimals,
prefix: prefix,
forcestepdivisibility : 'none',
booster : false
});
input.on('change', function(e){
scope.value = parseFloat(input.val());
if(scope.change) {
scope.change(scope.value);
}
if(!scope.$$phase) {
scope.$apply();
}
});
}
};
}); "[ngModel:numfmt] Expected I tried put that function into $apply too: scope.$apply(function() {
if(scope.change) {
scope.change(scope.value);
}
}); |
I'm getting this error now: Error: [ngModel:numfmt] Expected For the following markup:
So, input type="number" doesn't seem to be working in the table created using ng-repeat directive. |
I'll try to reply using e-mail: I'm getting an error using this html:
It seems like input type="number" is not working inside the ng-repeat On Fri, Feb 27, 2015 at 8:44 AM, André da Silva Medeiros <
|
Again, please provide a minimal reproducible example, either in a plnkr.co in a gist or even copy/ pasted here. Your snippets so far are impossible to debug. |
Are you able to create a table with ng-repeat directive and put a number Can you please start that plnkr and then I'll modify to add extra if needed? On Mon, Mar 2, 2015 at 4:21 PM, Martin Staffa [email protected]
|
You should be able to take it from here. Plnkr is pretty self-explanatory. |
Here is what I've done but I didn't reproduce the error. http://plnkr.co/edit/HBmOKTqQ9p4m3PzHNvds?p=preview On Mon, Mar 2, 2015 at 5:02 PM, Martin Staffa [email protected]
|
Having the same problem here (with 1.3.13), but again this is happening inside a large project and I'm not able to reproduce.
|
Can you add your directive code to that plunker @NaomiN ? |
There is a bug in numberInputType function of Angularjs. I use 1.3.6 and i have checked it in 1.3.14, same there. This part of code convert data from model format to view format when input type is number. ctrl.$formatters.push(function(value) { The checking if value is number and throwing error here make no sense at all. After that the value is cast to string :) Just comment it and everithing will work fine. It's work for me ;) |
What about replacing "(!isNumber(value))" with "(isNaN( parseFloat( value ) ) || !isFinite( value ))" ? |
OK, so the problem you are having is that You can work around this by adding a formatting directive to parse the string before it gets to the The question is whether the I would argue that this inconsistency is not desirable and a more consistent and reliable approach is to use the second directive (as shown in the plunker above) to do the conversion, allowing and ensuring your model stays consistently as a string. |
I updated the docs (both the |
Here is my plnkr with our directives demonstrating the issue |
@NaomiN - I don't see the error message in your Plunker. |
You can see the bug reproducing here http://plnkr.co/edit/qKC6YESYLbdDLe6bvNRM?p=preview This is especially annoying when you use strings to deal with floating point errors, and Angular doesn't let you mitigate the issue. +1 for @jamjamg 's solution #issuecomment-81638606 |
input[number] directives expect the model to be a number. If you are not passing a number to it then you must convert it first, which you can do with a directive. See this plunker: http://plnkr.co/edit/ZgewoGxx46LLDOieaso6?p=preview |
Hey @petebacondarwin , Regards, |
I had same issue, its sorted in latest version of angular, update angular to latest. |
@Bebokkk - as a workaround you can simply remove the parsers and formatters that were added by AngularJS's |
The docs also now link through to the error doc, which contains a runnable example of how to work around this restriction. Closes angular#11157 Closes angular#11334
I am getting the following error:
However, 0.00 is a number, so why would I see such an error?
The object generating this error is this:
The text was updated successfully, but these errors were encountered: