Non-angular error when saved, why?

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:

Does anyone know why you should give this warning?

    
asked by anonymous 31.07.2017 / 21:26

2 answers

5

In view you are setting agenda.dia to 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 type 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.

In order to preserve the hora value, you can use one of overloads of Date () that supports specifying hours and minutes:

agenda.hora = new Date(0,0,0,hora,min); 
    
31.07.2017 / 21:45
2

This happens because since version 1.3 of AngularJS, inputs with type date must have a ng-model that is a valid date.

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

The code should be

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

You read about it on the page regarding this error .

    
31.07.2017 / 21:48