在AngularJS中输入时,模型不是日期对象 [英] Model is not a date object on input in AngularJS

查看:49
本文介绍了在AngularJS中输入时,模型不是日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AngularJS,我试图使用输入 type = date :

Using AngularJS I am trying to display a date using an input type=date:

<input ng-model="campaign.date_start" type="date">

但是,这会出现以下错误:

However, this gives the following error:

Error: error:datefmt
Model is not a date object

日期实际上来自JSON API,格式如下:

The date actually comes from a JSON API in the following format:

date_start": "2014-11-19"

我认为我可以通过使用过滤器来解决它,但这不起作用,并且出现以下相同错误:

I thought that I could resolve it by using a filter, but this did not work and I get the same error with:

 <input ng-model="campaign.date_start | date" type="date">

我也尝试过将字符串转换为日期,但是再次出现相同的错误:

I have also tried converting the string to a date, but again I get the same error:

 $scope.campaign.date_start = Date(campaign.date_start);

我还能尝试什么?

推荐答案

您必须使用 Date 实例化 campaign.date_start ,而不能将其用作函数.

You have to instantiate campaign.date_start with Date not use it as a function.

它应该看起来像这样( 小型演示 ):

It should look something like this (small demo):

$scope.campaign.date_start = new Date(campaign.date_start);

这篇关于在AngularJS中输入时,模型不是日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆