如何使用角度日期管道从数字中获取月份名称 [英] How to get monthname from number using angular datepipe
本文介绍了如何使用角度日期管道从数字中获取月份名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过使用angular的日期管道显示一个包含(本地化)月份名称的下拉列表.
I want to display a dropdown containing (localized) month names by using angular's datepipe.
<select>
<option *ngFor="let month of months;" value="{{month}}">
{{month | date: 'MMMM'}}
</option>
</select>
months
是一个从0到11的数字数组.
months
is an array of numbers from 0 to 11.
生成的选项具有正确的值,但始终为一月"
The generated options have the correct values but it's always 'January'
<select>
<option value="0"></option>
<option value="1"> January</option>
<option value="2"> January</option>
<option value="3"> January</option>
<option value="4"> January</option>
<option value="5"> January</option>
<option value="6"> January</option>
<option value="7"> January</option>
<option value="8"> January</option>
<option value="9"> January</option>
<option value="10"> January</option>
<option value="11"> January</option>
</select>
我的代码是否有问题,还是根本不可能使用angular的datepipe实现我想要的东西?如果是这样,我该怎么做?
Is there something wrong with my code or isn't it possible at all to achieve what i want using angular's datepipe? If so, how can i accomplish it?
推荐答案
months必须是日期数组,可以使用
months must be an array of Dates, you can use
months=[0,1,2,3,4,5,6,7,8,9,10,11].map(x=>new Date(2000,x,2))
并且(请参见我将[value]更改为[value] ="i")
And (see that I changed the [value] to [value]="i")
<select>
<option *ngFor="let month of months;let i=index" [value]="i">
{{month | date: 'MMMM'}}
</option>
</select>
这篇关于如何使用角度日期管道从数字中获取月份名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文