- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我写了下面的代码片段,我认为它会禁用 FormArray
中的 FormControl
。
some.component.html
<form [formGroup]="testForm">
<div *ngFor="let num of countArr">
<input type="text" formNameArray="arrs">
</div>
</form>
some.component.ts
countArr = [1, 2, 3, 4, 5];
count = 5;
arrs;
testForm: FormGroup;
this.testForm = this.formBuilder.group(
arrs: this.formBuilder.array([])
);
this.arrs = this.testForm.get('arrs');
for (let i = 0; i < this.count; i++) {
this.arrs.insert(i, new FormControl({value: '', disabled: true}));
}
但是在for
执行完成后,我检查了表单,发现没有任何东西被禁用。你能告诉我我哪里做错了吗??? :-)
谢谢你的帮助!!! :-)
最佳答案
首先,您的 html 组件应该是这样的:
<form [formGroup]="testForm">
<div formArrayName="arrs">
<div class="form-group" *ngFor="let arrItem of testForm.get('arrs').controls; let i = index">
<input type="text" class="form-control" [formControlName]="i">
</div>
</div>
</form>
您不需要在 html 组件中重复一些随机的 count 变量。您可以迭代添加的控件。
您可能会问“具体是哪个控件?它们还没有添加!”
嗯,这就是为什么要在 ngOnInit
中以编程方式添加这些控件的原因:
ngOnInit() {
this.testForm = new FormGroup({
arrs: new FormArray([])
}
);
for (let i = 0; i < this.count; i++) {
const control = new FormControl(null, Validators.required);
(<FormArray>this.testForm.get('arrs')).push(control);
}
this.disableInputs();
}
这是启动 FormArray
的正确语法,然后在 for
循环内创建一个初始控件并将新创建的控件推送到您的数组。
注意:有一个 disableInputs()
函数调用。这也是您以编程方式禁用输入的地方:
disableInputs() {
(<FormArray>this.testForm.get('arrs'))
.controls
.forEach(control => {
control.disable();
})
}
工作示例:stackblitz
关于javascript - 如何在 Angular 4 的 FormArray 中禁用 FormControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51155763/
根据 the documentation , NLog 提供了一个 FormControl 目标,它将日志消息写入 Windows 窗体上控件的 Text 属性。但是,当我将 FormControl
以下是我的指令: restrict: 'E', scope: { }, templateUrl: 'directives/my.directive.html', link: function(scop
有没有办法在 Angular2+ 中声明表单模型时向 FormControl 添加任意元数据? 类似于: // In this fake example, {foo: 'bar'} is the me
如何在输入 FormControl 中显示一些不等于此 FormControl 值的文本? 例如,从服务器获取一些对象 - {id: 1, name: "Name1"} ,并设置 value Form
我正在研究 Angular Reactive 表单。这是我的组件类代码: ngOnInit(){ this.reviewForm = this.fb.group({
FormControl 在处理指令时遇到困难... 我正在尝试在我的输入字段中实现自动完成功能。我正在使用以下 Angular Material 指南,我逐字复制并粘贴了他们的 typescript
我正在使用 Angular 8 和 Reactive Forms。 在我的模板中,我有如下代码: 想法是,如果 validationError 为“truthy”,则在输入元素中添加红色边框。 无法
有没有办法检查是否需要控制? 当我实现一个专用的表单字段组件时出现了问题,该组件接受 FormControl 并且不仅有 输入 而且还有验证错误。由于某些字段是必需的,因此最好让用户知道 * 是否需要
FormControl 在处理指令时遇到困难... 我正在尝试在我的输入字段中实现自动完成功能。我正在使用以下 Angular Material 指南,我逐字复制并粘贴了他们的 typescript
我正在使用 Angular 8 和 Reactive Forms。 在我的模板中,我有如下代码: 想法是,如果 validationError 为“truthy”,则在输入元素中添加红色边框。 无法
我有一系列的项目。每个项目将通过使用 ng-repeat 和 ng-form 动态关联到一个表单。 ... 然后我就有可能创建新项目。但是当我这样做时,我希望他们的新形式变得变脏。 ADD
我想通过自定义指令将验证器动态添加到我的 FormControl。 @Directive({ selector: "[idNumber]", }) export class IdNumberD
我正在使用 Sematinc-UI 和 Angular2 ReactiveFormsModule 表单,我想使用 [formControl] 来选择多个。 如果我使用 select 它可以正常工作:
我现在已经根据一些逻辑从一个大型 FormGroup 中过滤掉了几个 FormControl - 我想知道如何合并/组合这些选定的 FormControl 并且只有一个订阅.. 我查看了 RxJS 文
我有 3 个字段与表单链接,类型为数字,当我修改字段 A 时,它会修改字段 B 和 C,当我修改字段 B 时,它会修改字段 A 和 C,当我修改字段 C 时,它会修改字段 A 和B相应地。 我的组件如
我有表单数组 FormGroup; let forms = [this.form1, this.form2]; 每个表单都有几个控件:FormControl。 如何向 FormControl 添加额外
我有一个使用自定义组件作为输入的表单,它接收一个 ControlName。当我在组件中键入内容时,它不会更新“错误”属性。 app-pf-input-text.ts: import { Compone
我讨论了表单应该如何与同事合作,我们对后端驱动的输入字段应该如何响应用户输入有不同的想法。 此讨论基于 Angular's definition of pristine "pristine" mean
我有包含 FormArray 的 FormGroup。由系统生成的 FormArray 中的每个 FormControls 如下所示: staffs: FormArray = { staffs
我使用的框架(Angular 7)。 我想像这样为 FormControl 设置一些类型。 startDate: FormControl; endDate: FormControl;
我是一名优秀的程序员,十分优秀!