简体   繁体   English

如何在日期字段中设置初始值或默认值?

[英]how to set initial value or default value in date field?

I am trying to implement a date picker in angularjs and bootstrap but I am facing a few issues: 我正在尝试在angularjs和bootstrap中实现一个日期选择器,但是我遇到了一些问题:

  • initial date is not set 初始日期未设置
  • when I am trying to open one date picker why does it open all the date pickers? 当我尝试打开一个日期选择器时,为什么要打开所有日期选择器?

Here is my code: http://plnkr.co/edit/elrOTfEOMmUkPYGmKTdW?p=preview 这是我的代码: http : //plnkr.co/edit/elrOTfEOMmUkPYGmKTdW?p=preview

$scope.dateOptions = {
  maxDate: new Date(2020, 5, 22),
  minDate: new Date(1970,1,1),
  startingDay: 1
};
function disabled(data) {
 return true
}

$scope.open1 = function() {
  $scope.popup1.opened = true;
};

$scope.formats = ['dd-MMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
$scope.popup1 = {
  opened: false
};

Why does it open all date pickers when only one is clicked? 为什么只单击一个时就会打开所有日期选择器? See image below. 参见下图。

在此处输入图片说明

initial date is not set 初始日期未设置

This is because you are setting the model to a string, but it requires an actual date object. 这是因为您将模型设置为字符串,但是它需要一个实际的日期对象。 Do this: 做这个:

date: {
  date: 'date',
  name: d // not this -> moment(d).format('DD-MMM-YYYY')
}

when I am trying to open one date picker why does it open all the date pickers? 当我尝试打开一个日期选择器时,为什么要打开所有日期选择器?

Since you're using ng-repeat to create multiple inputs, you need to also use separate properties for the is-open attribute on each input. 由于您使用ng-repeat创建多个输入,因此还需要为每个输入的is-open属性使用单独的属性。 I would suggest adding an opened property to the items you're repeating, like this: 我建议向要重复的项目添加一个opened属性,如下所示:

date: {
  date: 'date',
  name: d,
  opened: false
}

Then, on the click event of the button, pass in the repeated item: 然后,在按钮的click事件上,传递重复的项目:

ng-click="open1(x)"

Next, set the is-open attribute: 接下来,设置is-open属性:

is-open="x.opened"

Finally, set the opened property on it like this: 最后,像这样设置opened属性:

$scope.open1 = function(x) {
  x.opened = true;
};

Here is the plnkr. 这是plnkr。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM