Ich habe lange nach einer Lösung gesucht, aber nichts hat mir geholfen.Angular - Mungo: Kann Daten nicht manipulieren
Ich habe eine Angular JS App, die mit Mongoose und Express läuft.
Ich möchte das Datum als Objekt aus einem einfachen Formular speichern. Aber wenn ich mein Formular absende, werden Daten als String und nicht als Objekte gespeichert. So kann ich etwas nicht tun:
tache.start.getDate();
Hier ist meine Form ist:
<form name="formTache" ng-submit="addTache(tache)">
<div class="form-group row">
<div class="col-lg-12">
<input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/>
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date"/>
</div>
</div>
<div class="form-group row">
<div class="text-right col-lg-12">
<button type="submit" class="btn btn-default">Ajouter</button>
</div>
</div>
Hier ist mein Mongoose Schema:
var restful = require('node-restful');
var mongoose = restful.mongoose;
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var tachesSchema = new mongoose.Schema({
title : String,
start: {type: Date, default: new Date()},
});
var tachesModel = mongoose.model('taches', tachesSchema);
module.exports = restful.model('taches', tachesSchema);
Hier ist mein Controller :
angular.module('personaldashboard.tache', [])
.controller('TachesCtrl', function($scope, Taches, Progress, toaster) {
$scope.tache = new Taches();
var refreshTache = function() {
$scope.taches = Taches.query();
$scope.tache = ''
}
refreshTache();
$scope.addTache = function(tache) {
Taches.save(tache,function(tache){
refreshTache();
});
};
$scope.updateTache = function(tache) {
tache.$update(function(){
refreshTache();
});
};
$scope.removeTache = function(tache) {
tache.$delete(function(){
refreshTache();
});
};
$scope.editTache = function(id) {
$scope.tache = Taches.get({ id: id });
};
$scope.deselectTache = function() {
$scope.tache = ''
}
$scope.editTache_Projet = function(tache, projetId) {
tache.projet = projetId;
tache.$update(function(){
refreshTache();
});
};
});
Hier ist, was ich bekommen:
{ "_id": "58a99df24975ad0104c692b1", "title": "Test", "start": "2017-02-24T23:00:00.000Z" }
Warum erhalte ich eine Zeichenfolge wie "2017-02-24T23:00:00.000Z"
für mein Datum anstelle eines Objekts, während mein Mongoose Schema angeben start: {type: Date, default: new Date()}
?
Danke für Ihre Hilfe.
EDIT
Dank Saurabh Agrawal, habe ich versucht, das Datum zu konvertieren, wenn in der Steuerung submiting:
$scope.addTache = function(tache) {
tache.start = new Date(tache.start);
tache.end = new Date(tache.end);
Taches.save(tache,function(tache){
refreshTache();
});
};
Leider änderte dies nichts :(
ich ein Datum noch als String
"2017-02-20T23:00:00.000Z"
EDIT
Ich habe auch versucht, eine Richtlinie ändert
.directive("formatDate", function(){
return {
scope: {ngModel:'='},
link: function(scope) {
if (scope.ngModel) {
scope.ngModel = new Date(scope.ngModel);
}
}
}
})
und nenne es in meiner Form
<form name="formTache" ng-submit="addTache(tache)">
<div class="form-group row">
<div class="col-lg-12">
<input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/>
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date" formatDate/>
</div>
</div>
<div class="form-group row">
<div class="text-right col-lg-12">
<button type="submit" class="btn btn-default">Ajouter</button>
</div>
</div>
Aber nichts hinzuzufügen.
Irgendwelche anderen Ideen?
Jetzt ändern Sie es mit 'new Date()' –
Danke für die Antwort. Ich habe versucht, likes un my édition post gezeigt. Aber es funktioniert nicht. Dis Ich vermisse etwas? –