You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular Bootstrap4下*ngFor内绑定API值的Toggle开关无法切换问题

问题核心原因

  • ngFor循环内所有开关共用同一个formControlName="HasPermission",导致所有开关绑定同一个表单控件,状态互相冲突
  • 未给checkbox绑定选中状态,仅设置[value]无法控制checkbox是否勾选
  • 冗余的双span切换逻辑+错误的CSS规则,导致选中状态样式不生效

修正方案

1. 移除冗余CSS,保留基础toggle样式(admin.component.css)

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

input:checked + .slider {
  background-color: rgba(58,177,96,1);
}

input:focus + .slider {
  box-shadow: 0 0 1px rgba(58,177,96,1);
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

删除所有noPermission相关的样式代码,不需要单独定义未选中状态的类。


2. 修正模板代码(admin.component.html)

因为是循环渲染多个开关,需要用FormArray来管理多个表单控件,同时绑定选中状态:

<form [formGroup]="permissionForm">
  <div formArrayName="permissions">
    <div class="form-group card" *ngFor="let item of permissionsUserMap; let i=index">
      <div class="row p-1">
        <div class="col">
          <p class="d-inline">{{ item.permissionToolTip }} {{ item.hasPermission }}</p>
        </div>
        <div class="col">
          <label class="switch float-right mb-0">
            <input 
              #permissionToggle
              [formControlName]="i"
              type="checkbox" 
              (change)="onChangePermission($event,i)"
              [checked]="item.hasPermission === 'Y'"
            >
            <span class="slider round"></span>
          </label>
        </div>
      </div>
    </div>
  </div>
</form>

去掉冗余的双span切换逻辑,只保留一个slider span,用[checked]绑定API返回的状态,formControlName绑定循环索引对应FormArray里的控件。


3. 修正TS表单初始化逻辑(admin.component.ts)

初始化的时候把API返回的权限列表转成FormArray:

import { FormArray, FormControl, FormGroup } from '@angular/forms';

// 类内定义表单
permissionForm: FormGroup;

get permissions(): FormArray {
  return this.permissionForm.get('permissions') as FormArray;
}

// 拿到API返回的permissionsUserMap后初始化表单
initPermissionForm() {
  this.permissionForm = new FormGroup({
    permissions: new FormArray(
      this.permissionsUserMap.map(item => 
        new FormControl(item.hasPermission === 'Y')
      )
    )
  });
}

// 切换权限的处理逻辑
onChangePermission(event: Event, index: number) {
  const isChecked = (event.target as HTMLInputElement).checked;
  // 这里可以更新permissionsUserMap对应项的hasPermission值,提交的时候同步给后端
  this.permissionsUserMap[index].hasPermission = isChecked ? 'Y' : 'N';
}

内容的提问来源于stack exchange,提问作者carltonstith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 05:06:00