简体   繁体   English

如何在没有 jquery 的情况下在 Angular 页面加载时自动打开 HTML 日历

[英]How to open HTML calendar automatically on page load in Angular without jquery

I have used HTML input with the date field in the Angular application.I would like to open the date field modal on page load based on a condition without using jquery.我在 Angular 应用程序中使用了带有日期字段的 HTML 输入。我想在不使用 jquery 的情况下根据条件在页面加载时打开日期字段模式。 The condition is when the input is having a date selected then the calendar shouldn't be opened and if there is no date selected then the dropdown should be opened.条件是当输入选择了日期时,不应打开日历,如果未选择日期,则应打开下拉菜单。

I have tried setting onLoad in the HTML tag but i'm not sure how to close/open the calendar modal.我尝试在 HTML 标签中设置 onLoad,但我不确定如何关闭/打开日历模式。

<input
  [class.is-invalid]="deliveryDate.invalid && deliveryDate.touched"
  type="date"
  class="form-control"
  [ngClass]="{'alerts-border': isDateSelected()}"
  [min]="cartItems.minDeliveryDate"
  formControlName="delivery_date"
  onkeydown="return false"
/>

As described in this answer , the datepicker element is a kind of a separate window and it seems that you cannot call it directly.this answer中所述, datepicker 元素是一种单独的窗口,您似乎无法直接调用它。

So the answer is: no you cannot open the datepicker.所以答案是:不,你不能打开日期选择器。 If you have angular material installed then you can try their datepciekr.如果您安装了角材料,那么您可以尝试使用他们的 datepciekr。

I don't know if this is exactly what you want, but you could try this:我不知道这是否正是你想要的,但你可以试试这个:

<input
  [class.is-invalid]="deliveryDate.invalid && deliveryDate.touched"
  type="date"
  class="form-control"
  [ngClass]="{'alerts-border': isDateSelected()}"
  [min]="cartItems.minDeliveryDate"
  formControlName="delivery_date"
  [disabled]="form.value.delivery_date != null ? true : false"
  onkeydown="return false"
/>

NOTE : You didn't say the name of your form, I've put form as name ([disabled]=" form .value...). If your form has another name chage " form " by it.注意:您没有说出表单的名称,我已将表单作为名称 ([disabled]=" form .value...)。如果您的表单有另一个名称,请更改“表单”。

When you initialize the delivery_date field, you should initialize it to null if you want users could pick a date (or with a string like "2021-11-23" if you want to show the date but not let the user pick another one).当您初始化delivery_date字段时,如果您希望用户可以选择日期,则应将其初始化为null (如果您想显示日期但不让用户选择另一个日期,则应将其初始化为“2021-11-23”之类的字符串) .

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

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