在Angular中使用多个NGFOR时未定义的值?
我正在使用角度的多个ngfor循环。我想选择一个月和年范围。因此,有4个下拉菜单(从一年,从月到,年限到年)。问题在于,我在不同阶段也尝试使用多个NGFOR循环,但也会发生同样的问题。 1个NGFO循环在HTML中工作正常,但是使用多个NGFOR循环,只有1个工作正常,而其余的值不确定值。请看看,谢谢。
html代码:
<div>
<select name="monthf" id="monthf" [(ngModel)]="monthf" class="form-control" placeholder="monthf" >
<option [value]="month" *ngFor="let month of months">{{month}}</option>
</select>
<p>selected: {{monthf}}</p>
<select name="yearf" id="yearf" [(ngModel)]="yearf" class="form-control" placeholder="yearf" >
<option [value]="yearf" *ngFor="let year of years">{{year}}</option>
</select>
<p>selected: {{yearf}}</p>
<select name="montht" id="montht" [(ngModel)]="montht" class="form-control" placeholder="montht" >
<option [value]="montht" *ngFor="let month of months">{{month}}</option>
</select>
<p>selected: {{montht}}</p>
<select name="yeart" id="yeart" [(ngModel)]="yeart" class="form-control" placeholder="yeart" >
<option [value]="yeart" *ngFor="let year of years">{{year}}</option>
</select>
<p>selected: {{yeart}}</p>
</div>
component.ts代码(仅从.ts
文件中使用这些值,这就是为什么我只提到这些值):
months:string[]=['January','February','March','April','May','June','July','August','September','October','November','December'];
years:number[]=Array.from(Array(50)).map((_, i) => 2000 + i);//generate 100 years from 2000 to 2100
结果:
I am using multiple ngFor loops in Angular. I want to select Month and Year range. So there are 4 drop down menus (Month from, Year from, Month to, Year to). The problem is that I have tried using multiple ngFor loops before too in different stages, but same issue occurs. 1 ngFor loop works fine in HTML, but using multiple ngFor loops, only 1 works fine while the rest are having undefined values. Please have a look, Thankyou.
HTML Code:
<div>
<select name="monthf" id="monthf" [(ngModel)]="monthf" class="form-control" placeholder="monthf" >
<option [value]="month" *ngFor="let month of months">{{month}}</option>
</select>
<p>selected: {{monthf}}</p>
<select name="yearf" id="yearf" [(ngModel)]="yearf" class="form-control" placeholder="yearf" >
<option [value]="yearf" *ngFor="let year of years">{{year}}</option>
</select>
<p>selected: {{yearf}}</p>
<select name="montht" id="montht" [(ngModel)]="montht" class="form-control" placeholder="montht" >
<option [value]="montht" *ngFor="let month of months">{{month}}</option>
</select>
<p>selected: {{montht}}</p>
<select name="yeart" id="yeart" [(ngModel)]="yeart" class="form-control" placeholder="yeart" >
<option [value]="yeart" *ngFor="let year of years">{{year}}</option>
</select>
<p>selected: {{yeart}}</p>
</div>
Component.ts code(only these values are being used from the .ts
file that is why I only mentioned these values):
months:string[]=['January','February','March','April','May','June','July','August','September','October','November','December'];
years:number[]=Array.from(Array(50)).map((_, i) => 2000 + i);//generate 100 years from 2000 to 2100
Result:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论