【问题标题】:Angular js : manipulate value of objectsAngular js:操作对象的值
【发布时间】:2017-02-20 08:56:00
【问题描述】:

我有日期作为日期对象。

当我这样做时

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

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

我明白了

“2017-02-14T23:00:00.000Z”和 15

所以到这里为止,我们都很好。

但在我的情况下,当我尝试对另一个对象中的日期执行相同操作时,例如在此架构中:

 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>

什么都不显示。

我错过了什么?

编辑

我没有明确指出我想在 ng-repeat 中执行此操作

下面的代码给了我像“2017-02-20T23:00:00.000Z”这样的日期

<pre ng-repeat="tache in taches">
    {{tache.start}}
</pre>

下面的代码什么也没给我

<pre ng-repeat="tache in taches">
    {{tache.start.getDay()}}
</pre>

【问题讨论】:

    标签: javascript angularjs date object


    【解决方案1】:

    试试 {{tachesSchema.start.getDate()}}

    【讨论】:

    • 感谢您的帮助。我试过了,但它没有改变任何东西:(
    【解决方案2】:

    您可以使用函数定义默认值:

    new Schema({
        date: { type: Date, default: Date.now }
    })
    

    请参阅文档here

    【讨论】:

    • 感谢您的帮助。我试过了,但它并没有改变任何东西:(
    【解决方案3】:

    当您尝试 {{tache.start}} 时,您将获得“字符串”值。您应该将其转换为 Date 对象 (new Date("2017-02-20T23:00:00.000Z")) 并尝试 getDate() 方法。

    或者试试下面:

    new Schema({
        date: { type: Date, default: '15/02/2017' }
    });'
    

    【讨论】:

    • 当我这样做时 $scope.test = new Date([2017,2,15]);
      {{test}}
      {{test.getDate()}}
      我得到“2017-02-14T23:00:00.000Z”和 15 所以我相信“2017 -02-14T23:00:00.000Z" 可以作为对象进行操作。我错了吗 ?当我在 ng-repeat 中对我的真实对象“tache”尝试相同的事情时,我什么也得不到。这令人困惑:(
    • 请问您可以通过 jsfiddle / codepen / jsbin 分享您的示例代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多