在Angular中验证存在问题。带有电子邮件输入的复选框

在Angular中验证存在问题。带有电子邮件输入的复选框,angular,typescript,Angular,Typescript,我不知道是什么问题。不过,当我在localhost上运行时,我看到一条警告文本“需要电子邮件”。这是基于这样一个事实,即选中复选框时,应该需要电子邮件输入,但当未选中时=未选中 应用程序组件.ts import {Component,OnInit} from '@angular/core'; import {FormGroup,FormControl,FormBuilder,Validators} from '@angular/forms'; import {forbiddenNameValid

我不知道是什么问题。不过,当我在localhost上运行时,我看到一条警告文本“需要电子邮件”。这是基于这样一个事实,即选中复选框时,应该需要电子邮件输入,但当未选中时=未选中

应用程序组件.ts

import {Component,OnInit} from '@angular/core';
import {FormGroup,FormControl,FormBuilder,Validators} from '@angular/forms';
import {forbiddenNameValidator} from './shared/name.validator';
import {PasswordValidator} from './shared/password.validator';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

    get userName() {
        return this.registrationForm.get('userName');
    }
    get passPro() {
        return this.registrationForm.get('password');
    }
    get conPro() {
        return this.registrationForm.get('confirmPassword');
    }
    get email() {
        return this.registrationForm.get('email');
    }

    registrationForm: FormGroup;
    //adress: FormGroup;
    //Sposób 2 bardziej spakowany

    constructor(private fb: FormBuilder) {}

    ngOnInit() {
        this.registrationForm = this.fb.group({
            userName: ['', [Validators.required, Validators.minLength(512), forbiddenNameValidator(/password/)]],
            password: ['', [Validators.required, Validators.minLength(512)]],
            confirmPassword: ['', [Validators.required, Validators.minLength(512)]],
            adress: this.fb.group({
                name: [''],
                surname: [''],
                email: [''],
                dateB: [''],
                adres: [''],
                city: [''],
                postalCode: [''],
                subscribe: [false]
            })
        }, {
            validator: PassworrdValidator
        });

        this.registrationForm.get('subscribe').valueChanges
            .subscribe(checkedValue => {
                const email = this.registrationForm.get('email');
                if (checkedValue) {
                    email.setValidators(Validators.required);
                } else {
                    email.clearValidators();
                }
                email.updateValueAndValidity();
            });

    }
}
app.component.html的一部分-仅来自address:this.fb组

<h2>Dane użytkownika</h2>
<div formGroupName="adress">
    <div class="form-group">
        <label>Name</label>
        <input formControlName="name" type="text" class="form-control">
    </div>
    <div class="form-group">
        <label>Surname</label>
        <input formControlName="surname" type="text" class="form-control">
    </div>
    <div class="form-group">
        <label>Birthdate</label>
        <input formControlName="dateB" type="text" class="form-control">
    </div>
    <div class="form-group">
        <label>Adres</label>
        <input formControlName="adres" type="text" class="form-control">
    </div>
    <div class="form-group">
        <label>Postal Code</label>
        <input formControlName="postalCode" type="text" class="form-control">
    </div>
    <div class="form-group">
        <label>City</label>
        <input formControlName="city" type="text" class="form-control">
    </div>
    <div class="form-group">
        <label>Email adress</label>
        <input [class.is-invalid]="email.invalid && email.touched" formControlName="email" type="text"
            class="form-control">
        <small class="text-danger" [class.d-none]="email.valid || email.untouched">Email is required</small>
    </div>
    <div class="form-check mb-3">
        <input class="form-control-check" formControlName="subscribe" type="checkbox">
        <label class="form-check-label">
            Send me promotional offers.
        </label>
    </div>
    <div class="form-check mb-3">
        <input class="form-control-check" formControlName="subscribe" type="checkbox">
        <label class="form-check-label">
            I accept the terms and conditions.
        </label>
    </div>
</div>
Dane użytkownika
名称
姓
生日
阿迪斯
邮政编码
城市
电子邮件地址
电子邮件是必需的
给我发促销优惠。
我接受这些条款和条件。
  • TypeError:无法读取null的属性“无效”
  • AppComponent.ngonit处的属性“valueChanges”为null
  • 和上下文错误
  • 为什么会出现?天哪,为什么回车在这里不起作用

  • 您的“订阅”和“电子邮件”在表单组“地址”中,因此,您必须使用
    this.registrationForm.get('address').get('subscribe')
    this.registrationForm.get('address').get('email')
    Jezus,这太容易了……哦,我……上帝保佑你,或者干脆使用
    this.registrationForm.get('address.subscribe')