【问题标题】:How to make button stay active when clicked in Angular? I'm using *ngFor to show all my radio buttons在 Angular 中单击时如何使按钮保持活动状态?我正在使用 *ngFor 来显示我所有的单选按钮
【发布时间】:2023-02-01 20:22:16
【问题描述】:

我正在尝试显示多个单选按钮。我也能够在 .ts 文件中显示并获取保存值。但是当我点击按钮时,它会捕获值但不会像简单的单选按钮那样一直处于活动状态。我正在使用 Angular Reactive 表单

这是我的 HTML


<div class="btn-group">
   <label class="btn btn-outline-primary btn-color-change m-2" role="button" *ngFor="let gen of GenderType">
<input class="btn-check btn-color-change "  type="radio" [value]="gen" formControlName="Gender"><span>{{gen}}</span>
</label>

这是我的TS

import { Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup, FormBuilder,FormControl } from '@angular/forms';
import { TabsetComponent } from 'ngx-bootstrap/tabs';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
  @ViewChild('staticTabs', ) staticTabs?: TabsetComponent;
  
  GenderType=['Male','Female','TransGender'];
  Payments=['Yes','No']
  constructor(private fb:FormBuilder) { }

  ngOnInit(): void {
    
  }

  reactiveFormDemo=new FormGroup({
      Name:new FormControl(''),
      Email:new FormControl(''),
      Phone:new FormControl(''),
      Gender:new FormControl(''),
      Country:new FormControl(''),
      State:new FormControl(''),
      City:new FormControl(''),
      Payment:new FormControl('')
  })

  //Tabset components

 
  selectTab(tabId: number) {
    if (this.staticTabs?.tabs[tabId]) {
      this.staticTabs.tabs[tabId].active = true;
    }
  }
  getData()
  {
    console.log("submitted",this.reactiveFormDemo.value);
    console.log(this.reactiveFormDemo.controls['Gender'].value);
  }

  colorChange(){
    
    console.log("working!")
  }
}

【问题讨论】:

    标签: angular radio-button radio-group


    【解决方案1】:

    您可以将 [checked] 属性添加到输入元素并将其绑定到表单控件的值。这将确保选定的单选按钮即使在单击按钮后仍保持活动状态:

    <div class="btn-group"> 
      <label class="btn btn-outline-primary btn-color-change m-2" role="button" *ngFor="let gen of GenderType"> 
        <input class="btn-check btn-color-change "  type="radio" [value]="gen" formControlName="Gender" [checked]="form.controls['Gender'].value === gen"> 
        <span>{{gen}}</span> 
      </label>
    </div>
    

    【讨论】:

    • 嗨,爱德华,我之前试过这个,现在也试过了,它正在捕捉细节,但不能像普通的单选按钮一样工作。通常,如果我们选择一个,那么它会一直被选中,但在这种情况下,这不会发生
    【解决方案2】:

    你需要导入ReactiveForms模块并使用[表单组]="您的表单名称".

    import 'zone.js/dist/zone';
    import { Component } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { bootstrapApplication } from '@angular/platform-browser';
    import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
    
    @Component({
      selector: 'my-app',
      standalone: true,
      imports: [CommonModule, ReactiveFormsModule],
      template: `
        <form [formGroup]="myForm">
          <div class="btn-group">
            <label
              class="btn btn-outline-primary btn-color-change m-2"
              role="button"
              *ngFor="let gen of GenderType"
            >
              <input
                class="btn-check btn-color-change"
                type="radio"
                [value]="gen"
                formControlName="Gender"
              />
              <span>{{gen}}</span>
            </label>
          </div>
        </form>
      `,
    })
    export class App {
      name = 'Angular';
    
      GenderType = ['male', 'female', 'x', 'y'];
    
      myForm = new FormGroup({
        Gender: new FormControl(),
      });
    }
    
    bootstrapApplication(App);
    

    【讨论】:

    • 我也试过这个但没有用。同时我分享了我的 ts 和 html 代码。敬请期待
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2017-12-05
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多