【问题标题】:touched and valid using reactive forms in Angular在 Angular 中使用响应式表单触摸并有效
【发布时间】:2018-03-12 00:08:36
【问题描述】:

如何使用 Angular 4 中的响应式表单来使用已触摸和有效的属性。我在模板驱动表单中使用过,您可以将 <span class="text-muted" *ngIf="!fname.valid && fname.touched"> Please enter a valid first name</span> 放在输入字段下方。我还了解到响应式表单会更好,因为您必须在 component.ts 中编写逻辑。所以我希望它以反应形式实现,我被困在如何使用触摸和有效属性上。

html

<form [formGroup]="form" (ngSubmit)="onSignIn(form)">
    <div class="form-group">
        <input type="text" class="form-control" id="email" placeholder="Enter email" formControlName="email">
    </div>
    <div class="form-group">
        <input type="password" class="form-control" id="password" placeholder="Password" formControlName="password">
    </div><button class="btn btn-primary btn-block" type="submit" [disabled]="!form.valid">Sign In</button>
</form>

ts

 ngOnInit() {
    this.form = this.formBuilder.group({
      email: [null, [Validators.required, Validators.email]],
      password: [null, Validators.required],
    });
  }

  onSignIn(form: FormGroup){
    const email = form.value.email;
    const password = form.value.password;
    this.authService.loginUser(email, password)
    .subscribe(
      data => {
        this.router.navigate(['/settings']);
        alert("Login Successful");
        console.log(data);
      },
      error => {
        alert("Invalid Email or Password");
        console.log(error);
      });
  }

【问题讨论】:

  • 可能是这个:form.control.markAllAsTouched()

标签: angular angular-reactive-forms


【解决方案1】:

试试这个

<span class="text-muted" *ngIf="!form.controls['email'].valid && 
                 form.controls['email']?.touched"> Please enter a valid first name</span>

【讨论】:

    【解决方案2】:

    您可以以类似的方式使用它。要获取 FormControl,请在 FormGroup 对象上使用 get 方法,然后使用 hasError

    // in your template
    form.get('email').hasError('required') && form.get('email').touched
    form.get('email').hasError('email') && form.get('email').touched
    form.get('password').hasError('required') && form.get('password').touched
    

    您还可以在您的组件中为此创建一些不错的方法/getter。

    <form [formGroup]="form" (ngSubmit)="onSignIn(form)">
        <div class="form-group">
            <input type="text" class="form-control" id="email" placeholder="Enter email" formControlName="email">
          <span class="text-muted" *ngIf="form.get('email').hasError('required') && form.get('email').touched">Email is required</span>
        </div>
        <div class="form-group">
            <input type="password" class="form-control" id="password" placeholder="Password" formControlName="password">
             <span class="text-muted" *ngIf="form.get('password').hasError('required') && form.get('password').touched">Password is required</span>
        </div><button class="btn btn-primary btn-block" type="submit" [disabled]="!form.valid">Sign In</button>
    </form>

    【讨论】:

    • @wawka。 .hasError 的目的是什么?
    • @Joseph 您可以在您的 FormControl 上有多个验证器(在您的情况下,电子邮件有两个)。您可能希望为他们提供不同的错误消息。 hasError 允许您检查错误是否在特定验证器上。 valid 方法只告诉你其中一个验证失败。
    • @incognito ;) ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多