- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Angular 2 应用程序,它使用 ReactiveForms
模块来管理使用自定义验证器的表单。验证器接收一个 FormControl
对象。我有几个输入字段可以使用相同的自定义验证器,只要我在 FormControl
传递给验证器时知道字段的名称即可。
我在 FormControl
上找不到任何公开输入字段名称的方法或公共(public)属性。当然,很容易看出它的值(value)。下面显示了我想如何使用它:
public asyncValidator(control: FormControl): {[key: string]: any} {
var theFieldName = control.someMethodOfGettingTheName(); // this is the missing piece
return new Promise(resolve => {
this.myService.getValidation(theFieldName, control.value)
.subscribe(
data => {
console.log('Validation success:', data);
resolve(null);
},
err => {
console.log('Validation failure:', err);
resolve(err._body);
});
});
}
最佳答案
扩展 Radim Köhler 的回答。这是编写该函数的一种较短的方法。
getControlName(c: AbstractControl): string | null {
const formGroup = c.parent.controls;
return Object.keys(formGroup).find(name => c === formGroup[name]) || null;
}
关于angular - 如何从 Angular2 FormControl 对象获取输入字段的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40361799/
根据 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;
我是一名优秀的程序员,十分优秀!