比较验证器中与angular2的字段

我正在尝试在Angular2中创建具有“重复密码”功能的注册表。我希望使用自定义验证器作为表单控件来工作。

我遇到的问题是,当验证程序运行时,“ this-context”似乎已设置为验证程序,因此我无法访问RegistrationForm类上的任何本地方法。而且我似乎找不到从验证器访问ControlGroup的任何好方法。

有谁知道在自定义验证器中访问同一控件组中其他控件的好方法?

是该组件的简短示例:

import { Component, View } from 'angular2/angular2';
import { Validators, ControlGroup, Control, FORM_DIRECTIVES, FORM_BINDINGS } from 'angular2/angular2';

@Component({
    selector: 'form-registration'
})
@View({
    directives: [FORM_DIRECTIVES, ROUTER_DIRECTIVES],
    template: `
        <form (submit)="register($event)" [ng-form-model]="registerForm">
            <label for="password1">Password:</label>
            <input id="password1" ng-control="password1" type="password" placeholder="Passord" />

            <label for="password2">Repeat password:</label>
            <input id="password2" ng-control="password2" type="password" placeholder="Gjenta passord" />

            <button class="knapp-submit" type="submit" [disabled]="!registerForm.valid">Registrer deg</button>
        </form>
    `
})
export class RegistrationForm {
    registerForm: ControlGroup;

    constructor() {            
        this.registerForm = new ControlGroup({
            password1: new Control('', Validators.required),
            password2: new Control('', this.customValidator)
        });
    }

    public register(event: Event) {
        // submit form
    }

    private customValidator(control: Control) {
        // check if control is equal to the password1 control
        return {isEqual: control.value === this.registerForm.controls['password1'].value};
    }
}

因此,我通过将customValidator绑定到类的此上下文来解决该问题,如Sergio的评论中所述。

请记住,.bind(this)函数返回具有绑定上下文的新函数实例。

import { Component, View } from 'angular2/angular2';
import { Validators, ControlGroup, Control, FORM_DIRECTIVES, FORM_BINDINGS } from 'angular2/angular2';

@Component({
    selector: 'form-registration'
})
@View({
    directives: [FORM_DIRECTIVES, ROUTER_DIRECTIVES],
    template: `...my form template...`
})
export class RegistrationForm {
    registerForm: ControlGroup;

    constructor() {           
        this.customValidator = this.customValidator.bind(this); 
        this.registerForm = new ControlGroup({
            password1: new Control('', Validators.required),
            password2: new Control('', this.customValidator)
        });
    }

    public register(event: Event) {
        // submit form
    }

    private customValidator(control: Control) {
        // check if control is equal to the password1 control
        return {isEqual: control.value === this.registerForm.controls['password1'].value};
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular2中的跨字段验证

来自分类Dev

Angular2 ngModel验证器仅在组件中

来自分类Dev

条件表格验证器Angular2

来自分类Dev

比较 2 个输入字段的值以通过模板验证 Angular 7 中的表单

来自分类Dev

在 Angular2 中验证反应式表单字段的最佳方法是什么?

来自分类Dev

Angular2 中的按钮点击验证

来自分类Dev

如何逃脱angular2字段验证模式

来自分类Dev

如何逃脱angular2字段验证模式

来自分类Dev

将多个验证器添加到angular2的FormGroup中

来自分类Dev

从Angular2中的自定义验证器访问服务

来自分类Dev

如何在Angular2 / 4/5中实现自定义异步验证器

来自分类Dev

将多个验证器添加到angular2的FormGroup中

来自分类Dev

如何在Angular2中制作自定义验证器

来自分类Dev

Angular2自定义验证器未调用

来自分类Dev

Angular2模板驱动的异步验证器

来自分类Dev

Angular2自定义验证器未调用

来自分类Dev

angular2验证器错误:模板解析错误:(…)

来自分类Dev

Angular2中的拦截器

来自分类Dev

在Angular验证器中链接两个字段

来自分类Dev

在多个字段的 Angular 中设置验证器

来自分类Dev

在Angular2 FormGroup中验证动态添加的输入

来自分类Dev

在angular2中进行表单异步验证

来自分类Dev

Bootstrap 表单验证在 angular2 中不起作用

来自分类Dev

如何在 Angular2 中验证电子邮件?

来自分类Dev

侦听Angular2 / Typescript中对象字段的变化

来自分类Dev

复制angular2中的表单字段值

来自分类Dev

Angular2模板驱动的表单:如何为自定义表单控件创建字段验证?

来自分类Dev

结构化Angular2表单-如何包括可验证的条件表单字段

来自分类Dev

angular2中的装饰器在组件中做什么?

Related 相关文章

热门标签

归档