Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 将UI引导数据选择器作为字符串持久化到API_Angularjs_Asp.net Web Api_Json.net_Angular Ui Bootstrap_Angular Ui Datepicker - Fatal编程技术网

Angularjs 将UI引导数据选择器作为字符串持久化到API

Angularjs 将UI引导数据选择器作为字符串持久化到API,angularjs,asp.net-web-api,json.net,angular-ui-bootstrap,angular-ui-datepicker,Angularjs,Asp.net Web Api,Json.net,Angular Ui Bootstrap,Angular Ui Datepicker,我正在使用angular ui datepicker弹出窗口,并试图将保存的日期值持久化为仅包含日期的字符串年月日 日期选择后的输入是正确的,但存储的值是日期 2015年5月11日星期一00:00:00 GMT-0400(东部夏令时)上午12:00 是否有一种方法可以始终将值存储为日期字符串,而不是日期对象 <p class="input-group"> <input type="text" datepicker-popup="M/d/yyyy" ng-model="e

我正在使用angular ui datepicker弹出窗口,并试图将保存的日期值持久化为仅包含日期的字符串<代码>年月日

日期选择后的输入是正确的,但存储的值是日期

2015年5月11日星期一00:00:00 GMT-0400(东部夏令时)上午12:00

是否有一种方法可以始终将值存储为日期
字符串
,而不是
日期
对象

<p class="input-group">
    <input type="text" datepicker-popup="M/d/yyyy" ng-model="event.date"
        is-open="datePickers['date']" class="form-control" />
    <span class="input-group-btn">
        <button type="button" class="btn btn-default"
            ng-click="toggleDatepicker('date', $event)">
            <i class="fa fa-calendar"></i>
        </button>
    </span>
</p>
而不是

{
   date: `Mon May 11 2015 00:00:00 GMT-0400 (Eastern Daylight Time) 12:00AM`
}

我认为没有一个简单(标准)的JavaScript日期格式化函数,但我想您可以自己构建M/d/yyyy字符串,例如:

getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year
()


我自己也在一些项目上搞砸了,你可能想考虑一下。我发现Angular会自动将JSON中的ISO 8601字符串反序列化为实际的日期对象以供进一步使用,反之亦然,它还会透明地将日期对象序列化为这种格式,所以你自己不必担心这个问题。

我知道如何设置日期格式,但我正在尝试使用Angular UI自动执行此操作Datepicker@hunter对不起,我知道我不清楚,但这就是我在我的文章的后半部分所说的。我发现当使用日期对象作为模型值时,一切都很顺利,所以您只需担心将其作为字符串持久化。这取决于持久化的含义,但如果您使用$http从服务器存储/加载,它将自动转换为ISO8601字符串。因此,如果您可以使用日期对象作为模型(这似乎是angular ui datepicker首先需要的),那么一切都应该已经自动完成了。我将执行一个自定义指令,并利用ngModel$formatter将$modelValue设置为等于$viewValue,但不执行$render。没有测试,但这是我要尝试的。看这个问题/答案:嗯,我需要更多地研究角度指令来实现这一目标。任何指令操作模型而不影响其呈现方式的示例?请检查我编辑的注释中的问题。。。
getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year