如何在表单内的 Angular ngx-bootstrap 的日期选择器中更改日期格式 [英] How to change the date format in the datepicker of Angular ngx-bootstrap inside a form

查看:37
本文介绍了如何在表单内的 Angular ngx-bootstrap 的日期选择器中更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Angular 4 应用程序中使用 ngx-bootstrap Datepicker,

Using ngx-bootstrap Datepicker in an Angular 4 application,

我知道通常您可以通过这种方式指定日期格式:

I know that normally you can specify the date format this way:

<input id="from" name="from"
       bsDatepicker [(bsValue)]="myModel"
       value="{{ myModel | date:'yyyy-MM-dd'}}">

但是这次我在模板驱动的 Angular 表单中,所以我的输入没有绑定到像上面示例中的 myModel 这样的变量,但它只是绑定到表单:

but this time I'm inside a template-driven Angular form, so my input is not bound to a variable like myModel in the example above, but it's just bound to the form:

<input id="from" name="from"
       bsDatepicker ngModel>

那么在这种情况下如何指定日期格式?

so how can I specify the date format in this case?

编辑:看起来实现这一点的方法是在组件内创建一个新变量 newVar,然后将其与 [(bsValue) 绑定]="newVar":

Edit: it looks like that a way to achieve this is by creating a new variable newVar inside the component and then binding it with [(bsValue)]="newVar":

<input id="from" name="from"
       bsDatepicker ngModel
       [(bsValue)]="newVar"
       value="{{ newVar | date:'yyyy-MM-dd' }}">

但是我想知道是否有更好的解决方案.

However I wonder if there is a better solution.

推荐答案

[(bsValue)] 始终可用于 bsDatepicker,会不会是输入框

[(bsValue)] is always available for use with bsDatepicker, will it be input field or not

如果您只想更改一个字段的格式,则

if you want to change format of only one field then

[(bsValue)]="newVar" value="{{ newVar | date:'yyyy-MM-dd' }}" 没问题

bsDatepicker 与输入字段和表单集成,这应该适合您:

bsDatepicker has integration with input fields and forms, this should work for you:

并且您可以通过配置全局更改输入值中的日期格式

and you can globally change format of date in input's value via config

https://github.com/valor-software/ngx-bootstrap/blob/development/src/datepicker/bs-datepicker.config.ts#L31

或通过 [bsConfig]="{dateInputFormat: 'yyyy-MM-dd'}" 在元素上使用 bsDatepicker

or via [bsConfig]="{dateInputFormat: 'yyyy-MM-dd'}" on element with bsDatepicker

注意:L 格式目前存在一个问题 (v1.9.2),它总是使用 en-us 语言环境

note: there is currently an issue (v1.9.2) with L format, it`s always uses en-us locale

这篇关于如何在表单内的 Angular ngx-bootstrap 的日期选择器中更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆