- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个动态表单,您可以在其中动态添加表单,并动态添加子表单。例如:
+ user1
--- + color1
--- + color2
--- + color3
+ user2
--- + color1
--- + color2
--- + color3
+ user 3
--- + color1
您可以根据需要添加任意数量的用户,也可以为每个用户添加任意数量的颜色。我可以让第一个数组工作(用户),但不确定嵌套数组(颜色)。我已将其设置为在开始时加载用户和颜色。这是我到目前为止的代码:
export class FormsComponent implements OnInit {
myForm: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.myForm = this.fb.group({
email: '',
users: this.fb.array([])
})
}
get userForms() {
return this.myForm.get('users') as FormArray
}
get colorForms() {
return this.userForms.get('colors') as FormArray
}
addUser() {
const userGroup = this.fb.group({
user: [],
colors: this.fb.array([])
})
this.userForms.push(userGroup);
}
deleteUser(i) {
this.userForms.removeAt(i);
}
addColor() {
const colorGroup = this.fb.group({
color: []
})
this.colorForms.push(colorGroup);
}
deleteColor(i) {
this.colorForms.removeAt(i)
}
}
这是我的 html 代码:
<form [formGroup]="myForm">
<hr>
<input formControlName="email">
<div formArrayName="users">
<div *ngFor="let user of userForms.controls; let i=index" [formGroupName]="i">
<input formControlName="user">
<button (click)="deleteUser(i)">Delete User</button>
<div formArrayName="colors">
<div *ngFor="let color of colorForms.controls; let t=index" [formGroupName]="t">
<input formControlName="color">
<button (click)="deleteColor(t)">Delete Color</button>
</div>
</div>
</div>
</div>
<button (click)="addUser()">Add User</button>
</form>
显然这行不通,所以我试图理解我需要做什么。
最佳答案
它不起作用,因为您没有考虑存储控件的索引。
例如
get colorForms() {
return this.userForms.get('colors') as FormArray
}
不会工作,因为 userForms
返回 FormArray
并且您必须指定索引以获取属于特定用户的 colors
控件。
所以它可能看起来像:
getColors(index) {
return this.userForms.get([index, 'colors']) as FormArray;
}
在 html 中:
<div *ngFor="let color of getColors(i).controls;...>
在使用 colors
数组时,您还需要牢记这一点:
addColor(index: number) {
const colorGroup = this.fb.group({
color: []
})
this.getColors(index).push(colorGroup);
^^^^^^^^^^^^
use the them method to get correct FormArray
}
deleteColor(userIndex: number, colorIndex: number) {
this.getColors(userIndex).removeAt(colorIndex);
}
关于javascript - Angular 动态表单添加嵌套表单数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50901377/
我一直在开发一个 .NET 字符串格式化库来协助应用程序的本地化。它称为 SmartFormat 并在 GitHub 上开源. 它试图解决的问题之一是 Grammatical Numbers .这也称
leetcode关于单数II的题目是: 给定一个整数数组,除一个元素外,每个元素出现三次。找到那一个。笔记:您的算法应该具有线性运行时复杂度。你能在不使用额外内存的情况下实现它吗? 其实我已经从网站上
我想知道创建/命名模型的首选方法是什么? 我的应用程序有一个“用户”模型,其中包含用于创建、获取、更新(等)用户记录的所有相关业务逻辑。 在我的一些 Controller 中,我可能想要获取多个“用户
在我的 Mysql 数据库中,我有一个术语列表,例如(首字母大写,大多数时候是复数) Hairdressers Restaurants Beauty Salons Fournitures For Re
如果我决定为我的所有路线名称使用复数形式,但某些资源仅作为一个东西存在,您是否将其保持为单数(更直观)或尊重使用复数的决定并保持这种方式? 我们正在用 PHP 为我们的客户门户网站设计一个新的 API
我可能在做一些愚蠢的事情,但是...... 应用/模型/user.rb: class User 然后,当我导航到 /users/123/totem/new 时,出现错误: ActionView::
您能否澄清一些 Matplotlib 术语: “subplots”(或“subplot”?)这个词是“axes”的同义词吗? “轴”和“轴”的单数/复数是什么? 最佳答案 这确实是一个令人困惑的问题。
我有一个 profile我的应用程序中的模型。我想允许用户通过 /profile 查看他们自己的个人资料,所以我创建了这条路线: resource :profile, :only => :show 我
我是一名优秀的程序员,十分优秀!