Error in Angularjs when save date, why?

Asked

Viewed 52 times

2

I have the following HTML code:

<form name="formProfissional">
    <label>Data</label>
    <input class="form-control" type="date" name="dia" ng-model="agenda.dia" required>
    <label>Hora</label>
    <input class="form-control" type="time" name="hora" ng-model="agenda.hora" required>
    <button class="btn btn-block btn-primary btnAdicionar" ng-click="setAgenda(agenda)">Adicionar</button>
</form>

And this controller:

app.controller("AgendaProfissionaisCtrl", ['$scope', '$http', '$window', '$location', '$rootScope', '$routeParams', function ($scope, $http, $window, $location, $rootScope, $routeParams) {

$rootScope.idestabelecimento = localStorage.getItem('idestabelecimento');
$scope.idprofissional = $routeParams.idprofissional;

// $scope.nome;
var getPro = function(){
    var idpro = $scope.idprofissional;
    var opcao = 1; //Pegar profissional

$http.get("http://localhost:8888/sistemas/Android/areaAdmin/api/admin_estabelecimento/agendaProfissionais.php?opcao="+opcao+"&idpro="+idpro).success(function(response){
        $scope.nome = response.nome;
    })
}

getPro();

$scope.setAgenda = function(agenda){
    agenda.idpro = $scope.idprofissional;
    var mes  = new Date(agenda.dia).getMonth() + 1;
    var dia  = new Date(agenda.dia).getDate();
    var ano  = new Date(agenda.dia).getFullYear();

    var hora  = new Date(agenda.hora).getHours();
    var min  = new Date(agenda.hora).getMinutes();

    agenda.dia = ano+'-'+mes+'-'+dia;
    agenda.hora = hora+':'+min;
    console.log(agenda)
}


}]);

And on the console, although the correct data appears, this warning appears: inserir a descrição da imagem aqui

Does anyone know why this warning?

  • Angular version?

  • angular.js is v1.4.7 while angular.min.js is v1.5.7

2 answers

5


In the view you are defining agenda.dia as a date:

<input class="form-control" type="date" name="dia" ng-model="agenda.dia" required>

But with the line

agenda.dia = ano+'-'+mes+'-'+dia;

you are converting the object to a string. To keep the guy date, change to:

agenda.dia = new Date(ano, mes, dia);

If you want to preserve, for some reason, the string containing the description of the day, create another property.

To preserve the value hora, you can use one of the overloads Date() accepting hours and minutes specification:

agenda.hora = new Date(0,0,0,hora,min); 
  • 2

    Jeez, you’re back :p

  • @LINQ I never went, I just got in mode lurker to deliver a project. ;)

  • Beauty, and to do the same with the date, which is pointing the same error, would be: new Date(hour, min)?

  • @Gustavosevero I modified the answer by adding the relevant part to the property hora.

  • Gave this on console: Object {day: Mon Jul 31 2017 00:00:00 GMT-0300 (-03), time: Invalid Date, idpro: "1", option: 2}

  • @Gustavosevero Oops! Corrected. =)

  • Beautiful, but look at the month on the date... Object {day: Mon Jul 31 2017 00:00:00 GMT-0300 (-03), time: Sun Dec 31 1899 17:45:00 GMT-0200 (-02), idpro: "1", option: 2} I needed the month at kkkkk number

  • @Gustavosevero of the two, one: Or you preserve the Date object (which is actually just a numerical value indicating milliseconds from a point in the past called epoch) or separate values, as you did (for example agenda.dataDia, agenda.dataMes) - but not overwriting the expected property by the model (agenda.data).

  • But I’m not overwriting... Each date element I put into a separate variable.

  • 1

    @Gustavosevero at the time when agenda.hora = ano+'-'+mes+'-'+dia; is executed, the property is overwritten. setAgenda(agenda) is passing $Scope.agenda, the model, as reference.

Show 5 more comments

2

This is because from version 1.3 of Angularjs, inputs with type date need to have a ng-model which is a valid date.

The agenda.dia is a string, so this error happens.

The code should be

agenda.dia = new Date(ano, mes, dia);

You read about it on page related to this error.

Browser other questions tagged

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