AngularFormarrayselectindexcontrol
创始人
2024-10-24 22:01:33
0

在 Angular 表单中,当使用 Formarray 控件包含一组 FormGroup 时,如果需要对其中的 Select 控件进行索引控制,可以使用以下代码示例:

HTML 代码:

TypeScript 代码:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {

  form: FormGroup;

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.form = this.fb.group({
      formArray: this.fb.array([
        this.fb.group({
          mySelect: this.fb.control('first')
        }),
        this.fb.group({
          mySelect: this.fb.control('second')
        }),
        this.fb.group({
          mySelect: this.fb.control('third')
        })
      ])
    });
  }

  get formArray(): FormArray {
    return this.form.get('formArray') as FormArray;
  }

  prevOption(select: any) {
    const index = select.selectedIndex;
    select.selectedIndex = index > 0 ? index - 1 : select.options.length - 1;
  }

  nextOption(select: any) {
    const index = select.selectedIndex;
    select.selectedIndex = index < select.options.length - 1 ? index + 1 : 0;
  }

}

上述代码中,使用了两个按钮分别对应前一项和后一项,通过判断当前选中项的索引来进行控制。其中 prevOption 和 nextOption 方法均接收 Select 控件作为参数。

相关内容

热门资讯

实测发现!wepoker有什么... 实测发现!wepoker有什么规律(辅助挂)其实真的有挂(发现有挂辅助脚本)1、脚本辅助下载、免费透...
六分钟了解!hardrock作... 六分钟了解!hardrock作弊(辅助挂)本来有挂(存在有挂辅助器)1)辅助插件:进一步探索辅助大陆...
重大消息!wepoker透视脚... 重大消息!wepoker透视脚本(辅助挂)切实有挂(新版有挂辅助软件)破解侠是真的助透视。每个模型中...
玩家亲测!hhpoker脚本下... 玩家亲测!hhpoker脚本下载(辅助挂)切实存在有挂(有挂详情辅助脚本)1、点击下载安装,脚本分类...
必备科技!约局吧如何查看是否有... 必备科技!约局吧如何查看是否有挂(辅助挂)一直真的是有挂(有挂方针辅助插件)1、首先打开辅助器下载最...
必看攻略!hhpkoer辅助器... 必看攻略!hhpkoer辅助器视频(辅助挂)都是真的是有挂(有挂规律辅助攻略)进入游戏-大厅左侧-新...
分享一款!德普之星app安卓版... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
重要通知!hhpoker真的假... 重要通知!hhpoker真的假的(辅助挂)真是有挂(证实有挂辅助脚本)1、每一步都需要思考,不同水平...
记者揭秘!微扑克游戏辅助器(辅... 记者揭秘!微扑克游戏辅助器(辅助挂)原来是有挂(有挂规律辅助app)1、实时透视辅助更新:用户可以随...
记者揭秘!德普之星透视辅助软件... 记者揭秘!德普之星透视辅助软件激活码(辅助挂)其实是真的有挂(有挂方式辅助攻略)1、模拟器是什么优化...