如何在使用angularjs UI的日期时设置的初始值NG-模型模型、日期、初始值、如何在

2023-09-13 04:54:23 作者:下载一颗空白心

我的模型是基于掀起了JSON对象和日期来作为2012-12-13T00:00:00-07:00

My model is based off of a JSON object and the date comes in as 2012-12-13T00:00:00-07:00

我要使用的日期选择器UI的日期。

I want to use the date picker ui-date.

我有以下的code的作品。

I have the following code that works.

<input ng-model="course.startDate" value="{{item.startDate | date: 'MM/dd/yyyy'}}" ui-date ng-required="true">

不过,我不认为我应该使用值={{item.startDate |日期:'MM / DD / YYYY'}}部分但是没有它的输入不从模型中值加载。

However,I don't think I should be using the value="{{item.startDate | date: 'MM/dd/yyyy'}}" part but without it the input doesn't load with the value from the model.

该模型的约束,并正常工作时,我选择了一个日期,它只是显示了空当加载页面。

The model is bound and functions correctly when I chose a date, it just shows up empty when the page loads.

什么是初始化此输入正确的方法是什么?

What is the correct way to initialize this input?

推荐答案

使用 UI的日期 NG-模型应该是足够了:

<div ng-controller="MyCtrl">
  <input ng-model="date" ui-date>
</div>

前提是 MyCtrl 的定义是这样的:

function MyCtrl($scope) {
    $scope.date= new Date();
}​

下面是一个的jsfiddle: http://jsfiddle.net/MvGFF/2/

Here is a jsFiddle: http://jsfiddle.net/MvGFF/2/

你所必须知道的是, UI的日期 预计模型值为日期的实例,也许你正在传递一个字符串(或其他类型的)价值?

What you must know is that the ui-date expects model value to be an instance of Date, maybe you are passing a string (or other type) value?

如果您的模型认为是字符串值,但您仍想使用日期选择器,你需要添加 UI的日期格式指令就像这个例子:

If your model holds values that are strings and you still want to use a date-picker you need to add the ui-date-format directive like in this example:

<input ng-model="date" ui-date ui-date-format>

其中控制器的定义,像这样:

where the controller is defined like so:

function MyCtrl($scope) {
    $scope.date= "2012-12-13T00:00:00-07:00";
}​

下面是一个的jsfiddle: http://jsfiddle.net/d4xz2/1/

Here is a jsFiddle: http://jsfiddle.net/d4xz2/1/