Check out this fiddle: http://jsfiddle.net/bwjbz/6/
I'm trying to strictly enforce positive whole numbers. I have tried several avenues, and I can't seem to get what I'm looking for. (If you try a couple of times you can end up getting a non-number or negative number to stick in the input).
In addition to the above fiddle, I have also tried using the number filter and also tried creating my own directive.
Number filter:
<input type="number" min="0" max="1000" value="{{itm.qty | number:0}}" required data-ng-model="itm.qty" data-ng-change="setQty()" data-whole-number>
You'll notice the data-whole-number directive. I'm not fully comfortable with directives yet, but this is it:
app.directive('wholeNumber', function() {
return function(scope, elem, attrs) {
elem.on("blur", function() {
var num;
num = parseInt(elem.val(), 10);
num = Math.abs(num);
scope.$apply(elem.val(num));
});
};
});
The directive itself does the correct DOM manipulation, but the model doesn't update with the new value.
So there are two aspects to this questions:
The fiddle works to set the correct value on the model, but the model text doesn't update. However, in another method, I set the qty of the model itm to 1 and it changes both the model and the visible value. You'll notice it does in fact change the model correctly (notice the bindings, number of footballs = 1, when value is set to 1.56, for example).
Why isn't the directive propagating the changes to the model?
Thanks so much in advance, -Brian