Data in ng-Model with input date shows error in Angularjs

Asked

Viewed 1,001 times

1

Good evening, I’m trying to accomplish the CRUD in my application, using AngularJS and WEBService, when I will change a certain driver’s data, I send the JSON from the same to the SessionStorage, on the change page, access the object as well as its attributes through the ng-model of inputs.

When it comes to dates, the input is filled in with data from SessionStorage, however, when it comes to the dates, the AngularJS displays the following console error:

Error: [ngModel:datefmt].

Would anyone know how to fix this mistake? I couldn’t find answers until then. Thanks in advance!

My input:

<label> Data de validade </label>
<input type="date" ng-model="motorista.data_vencimento" id="codigo_motorista" />

My method that sends the object in format JSON to the SessionStorage:

$scope.enviarDados = function(motorista){
    $scope.jsonObj = angular.toJson(motorista, false);
    window.sessionStorage.motorista = JSON.stringify(motorista);
    window.location.href = "formAlterarMotorista.jsp";
}

2 answers

1

The date format you need to pass to input recognises how a valid date format should be:

YYYY-MM-DD //ano-mes-dia

Therefore, you must format the date before assigning to the field.

  • thanks for answering...how could I convert this "string data" to this format? I would do it where? In the controller or in my HTML?

  • my json is coming like this: {"id":13,"address":{"id_address":37,"street":"Av Taquaritinga","numero":188,"neighborhood":"Jd América","complement":"Next to Vaz Filho","cep":"14811223","city":"Araraquara","state":"São paulo","parents":"Brasil"},"provider":{"id":1,"name_fantasy":"test","razao_social":"test"},"code":"TEST","name":"Matheus Simões Minguini","observation":"Testing the change","rg":"404204065","Cpf":"45509939877","emissao_rg":"2017-02-13","orgao_expedidor_rg":"SSP","email":"[email protected]","telephone":"1633379410","cellular":"16997063747","cnh":"4564564645"{

1

What is happening is that Angular is not recognizing the variable motorista.data_vencimento as a valid date field, for this purpose when loading the variable motorista you can treat the field like this

$scope.motorista.data_vencimento = new Date(data_vencimento);

where data_vencimento is the value you possibly carry from the webservice.

  • knows why the value works with string, but the ng-model nay?

Browser other questions tagged

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