在Angular中使用多个NGFOR时未定义的值?

发布于 2025-01-24 21:59:04 字数 1584 浏览 3 评论 0原文

我正在使用角度的多个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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文