2017-02-19 9 views
0

私は解決のために長い間探してきましたが、何も助けてくれませんでした。角度 - マングース:日付を操作できません

私はMongooseとExpressで動作するAngular JSアプリを持っています。

単純なフォームからオブジェクトとして日付を保存したいと思います。 私のフォームを提出すると、日付はオブジェクトとしてではなく文字列として保存されます。 は、だから私はのような何かを行うことはできません。ここで

tache.start.getDate(); 

を私のフォームである:ここでは

<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> 

は私のマングースのスキーマである:ここで

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); 

は私のコントローラであります:

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(); 
     }); 
    }; 
    }); 

ここでは、私が得るものです:

{ "_id": "58a99df24975ad0104c692b1", "title": "Test", "start": "2017-02-24T23:00:00.000Z" } 

私のマングーススキーマがstart: {type: Date, default: new Date()}を指定するのに対し、なぜ私が代わりに、オブジェクトの私の日付の"2017-02-24T23:00:00.000Z"のような文字列を得るのですか?

ありがとうございました。

EDIT Saurabh Agrawalさんへ

おかげで、私はコントローラにsubmitingたときに日付を変換してみました:

$scope.addTache = function(tache) { 
    tache.start = new Date(tache.start); 
    tache.end = new Date(tache.end); 
    Taches.save(tache,function(tache){ 
    refreshTache(); 
    }); 
}; 

は悲しいことに、これは何も変わっていない:(

私はまだのように日付を持っています文字列

"2017-02-20T23:00:00.000Z" 

EDIT

私はまた、ディレクティブ

.directive("formatDate", function(){ 
    return { 
    scope: {ngModel:'='}, 
    link: function(scope) { 
     if (scope.ngModel) { 
      scope.ngModel = new Date(scope.ngModel); 
     } 
    } 
    } 
}) 

を追加し、

<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> 

私の形でそれを呼び出そうとしましたが、何も変化します。

他のアイデアはありますか?

+0

は、 'new Date()'を使って変更しました –

+0

あなたのお返事ありがとうございます。私は好きな人に私の投稿を表示してみました。しかし、それは動作しません。私は何かが欠けている? –

答えて

0

検索すると、本当の問題が見つからないことがわかりました。

私の日付は日付オブジェクトです。

私はこの

$scope.test = new Date([2017,2,15]); 

<pre>{{test}}</pre> 
<pre>{{test.getDate()}}</pre> 

を行うと、私は

「2017-02-14T23を取得:00:00。000Z」と15

のように表示するので、日付 『2017-02-14T23:00:00.000Zは』

オブジェクトですが、私は日付whitchと同じことをしようとすると、私の場合には、他にありこのスキーマ内のようなオブジェクト:

var tachesSchema = new mongoose.Schema({ 
    title : String, 
    start: {type: Date, default: new Date()}, 
    end: {type: Date, default: new Date()}, 
    comment : String, 
    state : Boolean, 
    projet : { type: ObjectId, ref: 'Projets' } 
}); 

私は何を取得:(

このコードは:

{{tache.start}}

はトンのような日付を表示彼の "2017-02-20T23:00:00.000Z"

しかし

<pre>{{tache.start.getDate()}}</pre> 

は何も表示されません。

私が見逃したことはありますか?

関連する問題