Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在角度为4的选择框中处理optgroup大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在角度4的选择框中获取onchange函数中的optgroup标签

我有一个表格中的选择框,其中有多个日期作为选项组,时间为24小时格式,分别为1000,1200和1400,分别为10 AM12PM和2Pm.

<SELEct class="form-control" formControlName="arrival_time" (changE)="onSlotChange($event)">
                     <option value="" data-date="" data-slot="">SELEct Arrival Time</option>
                     <ng-container *ngIf="!availablePrevSlots">
                        <optgroup label="{{availablePrevSlotDatE}}">
                           <option value=null hidden>-- No Slot Available --</option>
                        </optgroup>
                     </ng-container>
                     <ng-container *ngIf="availablePrevSlots">
                        <optgroup label="{{availablePrevSlotDatE}}">
                           <option value=null hidden>HH:MM</option>
                           <option *ngFor="let slot of availablePrevSlots" [value]="slot">{{slotToString(slot)}}</option>
                        </optgroup>
                     </ng-container>
                     <ng-container *ngIf="!availableSlots">
                        <optgroup label="{{availableSlotDatE}}">
                           <option value=null hidden>-- No Slot Available --</option>
                        </optgroup>
                     </ng-container>
                     <ng-container *ngIf="availableSlots">
                        <optgroup label="{{availableSlotDatE}}">
                           <option value=null hidden>HH:MM</option>
                           <option *ngFor="let slot of availableSlots" [value]="slot">{{slotToString(slot)}}</option>
                        </optgroup>
                     </ng-container>
                     <ng-container *ngIf="!availableNextSlots">
                        <optgroup label="{{availableNextSlotDatE}}">
                           <option value=null hidden>-- No Slot Available --</option>
                        </optgroup>
                     </ng-container>
                     <ng-container *ngIf="availableNextSlots">
                        <optgroup label="{{availableNextSlotDatE}}">
                           <option value=null hidden>HH:MM</option>
                           <option *ngFor="let slot of availableNextSlots" [value]="slot">{{slotToString(slot)}}</option>
                        </optgroup>
                     </ng-container>
                  </SELEct>

现在我使用onchange函数获取所选的时间/点值,但我也想获得选项标签
有没有方法可以获得optgroup标签

一旦我们提交表单,我们在数据库中保存插槽,在编辑页面上我们用数据绑定显示它,但由于多个optgroup可以有相同的插槽,它总是显示一个选择的optgroup值

不确定如何显示此处选择的确切optgroup值.

解决方法

可以使用VAnillaJS来实现这一点,在您的更改功能中写下:

const SELEctedIndex = ev.target.SELEctedIndex;
const optGroupLabel = ev.target.options[SELEctedIndex].parentNode.getAttribute('label');

这是一个有效的stackBlitz:https://stackblitz.com/edit/angular-64vvdn

大佬总结

以上是大佬教程为你收集整理的在角度为4的选择框中处理optgroup全部内容,希望文章能够帮你解决在角度为4的选择框中处理optgroup所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。