Working with Data on ngInit

Asked

Viewed 268 times

1

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

When the user enters the screen, I already bring the date in the following way: 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 Django value in the input, it seems that the ng-model overwrites and leaves everything blank in the field. So I always use NG-INIT to initialize the 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

1 answer

1


Expected `1996-10-01` to be a date

As the error, you are inserting a String, but an object of the type Date. Simply via javascript convert, example: new Date('1996-10-01').

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

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

and in the controller:

$scope.converter = function(s){
    $scope.myobject.date = new Date(s);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.