简体   繁体   English

使用PrimeNG日历检测[(ngmodel)更改为所选日期

[英]Detect [(ngmodel) change to selected date with PrimeNG calendar

I have an input element with an attached datepicker created using PrimeNG calendar. 我有一个输入元素,带有使用PrimeNG日历创建的附加日期选择器。

HTML Part HTML部分

<p-calendar showAnim="slideDown" 
            id="calendar" 
            [showIcon]="true 
            [(ngModel)]="myDate" 
            (blur)="onBlurMethod($event)">
</p-calendar>

Here I have used PrimeNG calendar tag. 在这里,我使用了PrimeNG日历标签。 I have used one hidden text box so I can pick the updated value but it is reflecting in the text box. 我使用了一个隐藏的文本框,因此可以选择更新的值,但它会反映在文本框中。 How can I capture the updated value on any event ? 如何捕获任何事件的更新值?

<input type="hidden" id = "datePickerText" [ngModel]="myDate">

When the user changes the date by typing directly into the input element, I can get the value with the input element's blur event, as shown above. 当用户通过直接在输入元素中键入来更改日期时,我可以通过输入元素的blur事件获取值,如上所示。 But when the user changes the date from datepicker (ie by clicking a date on the calendar), the blur handler is not called. 但是,当用户从日期选择器更改日期时(即通过单击日历上的日期),则不会调用模糊处理程序。

How can I detect changes to the selected date that are made via the datepicker , rather than by typing into the input element ? 如何检测通过datepicker而不是通过输入输入元素对选定日期所做的更改?

The PrimeNG calendar has two events you can use: onBlur and onSelect . PrimeNG日历有两个事件可以使用: onBluronSelect

So you probably want to change your code to use onBlur instead of blur : 因此,您可能想更改代码以使用onBlur而不是blur

<p-calendar showAnim="slideDown" 
            id="calendar" 
            [showIcon]="true"
            [(ngModel)]="myDate" 
            (onBlur)="onBlurMethod($event)">
</p-calendar>

您可以使用(ngModelChange)="yourMethod()"

Another approach can be to use get and set properties to detect a change directly. 另一种方法是使用getset属性直接检测更改。

private _startDate: Date;

set startDate(date: Date)
{
    this._startDate = date;
    alert('changed');
}

get startDate(): Date
{
    return this._startDate;
}

Whichever way you choose you'll need to make sure you know when the logic is being triggered - and that it isn't being ran unnecessarily. 无论选择哪种方式,都需要确保知道何时触发逻辑-并且不会不必要地运行它。 This way may get cumbersome and have separation of concern issues and is probably not advisable if you have to make a remote call as a result of the trigger. 这种方式可能很麻烦,并且使关注点分离,如果由于触发而必须进行远程呼叫,则不建议这样做。

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

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