My form data is returned from a REST call. Example data is:
{
id: 4
version: 3
code: "ADSFASDF"
definition: "asdflkj"
value: "1234"
active: "false"
formula: false
validTo: "2014-12-31T05:00:00"
validFrom: "2010-12-31T10:00:00"
}
I'm running into trouble using input[number] and input[date] fields as they require the data to be a number or a date respectively and not a string. Similar problems occur for booleans (check boxes), etc.
I thought I could get around it using a $formattter, but the value passed to the formatter is always "". I presume this means the that $formatter is called after Angular has already tried to parse the data model.
Without having to initially cast everything in my controller, is there a way to cast the data via a directive, or within the HTML tag directly?
Ex:
<input type="number" class="form-control" ng-model="charge.value" jk-numeric id="chargeValue"/>
$formatter:
app.directive( 'jkNumeric',
function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
var stringToNum = function(text) {
if( angular.isNumber(text) )
return text;
else
return parseInt( text, 10);
}
// assign the parser and the formatter to the model
ngModelCtrl.$formatters.unshift(stringToNum);
}
};
});