Working with Data in ngInit

1

I have an input on my date form. Whenever the user modifies the date, it will be updated in Django and will return another date.

When the user enters the screen, I already bring the date as follows: 1996-10-01

Input

<input type="date" ng-model="myobject.date" ng-change="updateField()" ng-init="myobject.date = '{{djangoobj.date}}'">

When I try to just put the value of django in the input, it seems like ng-model overrides and all goes blank. So I always use NG-INIT to initialize ng-model with the value of Django. However, when I try to do this with a date field, I get the following error message:

Expected '1996-10-01' to be a date
    
asked by anonymous 31.10.2017 / 16:27

1 answer

1
Expected '1996-10-01' to be a date

Depending on the error, you are entering a String , but an object of type Date is expected. Just via javascript convert, example: new Date('1996-10-01') .

The ideal is to do all this via Ajax, receive data and feed $scope . In the form you're doing, you could put ng-init a function to convert String to Date and update ng-model .

<input type="date" ng-model="myobject.date" ng-change="updateField()" ng-init="converter('{{djangoobj.date}}')"

and no controller:

$scope.converter = function(s){
    $scope.myobject.date = new Date(s);
}
    
31.10.2017 / 16:35