Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using ng-repeat to cycle through my returned fields. Every field can have few types, e.g. TextField, NumericField, also DateTimeField.

So I'm looking which type it is and showing that needed input:

<div ng-repeat="field in fields">
    <div ng-show="field.fieldtype == 'TextField'">
        <input type="text" ng-model="DATA.values[field.key]" />
    </div>

    <div ng-show="field.fieldtype == 'NumericField'">
        <input type="number" ng-model="DATA.values[field.key]" />
    </div>

    <div ng-show="field.fieldtype == 'DateTimeField'">
        <datepicker ng-model="DATA.values[field.key]"></datepicker>
        <timepicker ng-model="DATA.values[field.key]"></timepicker>
    </div>
</div>

Everything was working as expected until I added datepicker and timepicker. Now my model gets formatted all the time even field type is not DateTimeField, and when entering a value (e.g. in a TextField), I'm getting errors:

Timepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.

Can I tell somehow to those pickers to not format my model or is there any other solution?

share|improve this question
    
What does DATA.values[field.key] contain exactly? –  Jeroen Noten Jul 29 at 15:43
    
It stores entered value. If field is described as TextField, so it's a string, if it's NumericField - it should be a number. But I'm not formatting this model anyhow myself - I just do I simple validation afterwords. And seems like date/time pickers formats that field into Date/Time format. –  Julius Jul 29 at 15:45
    
if you dont want valid date/time, then use input[text] –  harish Jul 29 at 15:53
    
Can I make it inline (embedded) when using input[text]? –  Julius Jul 29 at 15:57

1 Answer 1

You should pass a Date object as model. In the controller, try something like the following:

$scope.DATA = { values: {} };
angular.forEach(fields, function (field) {
    switch (field.fieldtype) {
        case 'DateTimeField';
        case 'NumericField';
            var date = new Date();
            $scope.DATA.values[field.key] = date.toISOString();
            break;
        case 'TextField'
            $scope.DATA.values[field.key] = '';
            break;
    }
});

This sets the date to 'now' for each datepicker and timepicker, and to an empty string for a text field.

When you need to send it to a server or something, you have to change it to a string or number when sending the request.

share|improve this answer
    
But I want to do the opposite - I don't want my field to be formatted as a date. –  Julius Jul 29 at 15:52
    
What format do you want then? It is a date picker.. so the input and output is date.. When you need to send it to a server or something, you have to change it to a string or number when sending the request. –  Jeroen Noten Jul 29 at 15:53
    
Simple text... So that my model doesn't puts many errors if a string is entered in it. –  Julius Jul 29 at 15:58
    
Updated my answer. It is a string now, following the ISO 8601 date/time format. See also en.wikipedia.org/wiki/ISO_8601 –  Jeroen Noten Jul 29 at 16:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.