首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ExpressionChangedAfterItHasBeenCheckedError:表达式在检入angular后已更改

ExpressionChangedAfterItHasBeenCheckedError:表达式在检入angular后已更改
EN

Stack Overflow用户
提问于 2018-05-28 18:42:45
回答 1查看 669关注 0票数 0

我是angular的初学者,我正在尝试使用代码构建表单验证我的要求是当我点击 reset 按钮域需要重置为此我遵循了下面的代码但问题是当我点击Reset按钮时我在exception下面可以有人帮我吗我在哪里做了mi-stack

home.html:

代码语言:javascript
复制
<ion-content padding>

  <h1>Authentication Demo</h1>
  <hr align="left" width="30%">

  <form [formGroup]="authForm">
    <ion-item>
      <ion-label floating>Username</ion-label>
      <ion-input type="text" [(ngModel)] ="username" value="" [formControl]="authForm.controls['username']" [ngClass]="{'error-border':!authForm.controls['username'].valid}"></ion-input>
    </ion-item><br/>
    <div class="error-box" *ngIf="authForm.controls['username'].hasError('required') && authForm.controls['username'].touched">* Username is required!</div>
    <ion-item>
      <ion-label floating>Password</ion-label>
      <ion-input type="password" [(ngModel)] ="password" value="" [formControl]="authForm.controls['password']"></ion-input>
    </ion-item><br/>
    <div class="error-box" *ngIf="authForm.controls['password'].hasError('required') && authForm.controls['password'].touched">* Username is required!</div>
    <div class="error-box" *ngIf="authForm.controls['password'].hasError('minlength') && authForm.controls['password'].touched">* Minimum username length is 8!</div>
    <div class="error-box" *ngIf="authForm.controls['password'].hasError('checkFirstCharacterValidatorOutput') && authForm.controls['password'].touched">* Password cant't start with number!</div><br/>
    <ion-list>
      <ion-item>
        <ion-label>Gender</ion-label>
        <ion-select [formControl]="authForm.controls['gender']">
          <ion-option value="e">Other</ion-option>
          <ion-option value="f">Female</ion-option>
          <ion-option value="m">Mele</ion-option>
        </ion-select>
      </ion-item>
    </ion-list><br/>

    <button type="submit" ion-button full [disabled]="!authForm.valid" (click)="myFunc1()">Submit</button>
    <button type="submit" ion-button full [disabled]="!authForm.valid" (click)="myFunc2()">Reset</button>

  </form>

</ion-content>

home.ts:

代码语言:javascript
复制
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {FormBuilder, FormGroup, Validators, AbstractControl} from '@angular/forms';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  username:string;
  password:string;

  authForm : FormGroup;
  constructor(public navCtrl: NavController, public navParams: NavParams, private fb: FormBuilder) {
    this.authForm = fb.group({
          'username' : [null, Validators.compose([Validators.required])],
          'password': [null, Validators.compose([Validators.required, Validators.minLength(8)])],
          'gender' : 'e'
        });
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad');
  }

  myFunc1(){
    console.log('Form submitted!')
  } 

  myFunc2(){
    this.username='';
    this.password='';
  }
}

例外:

ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。上一个值:'error-border: false‘。当前值:'error-border: true‘。

EN

回答 1

Stack Overflow用户

发布于 2018-05-28 18:52:36

在重置函数myFunc2中,使用内置的表单重置功能:

代码语言:javascript
复制
myFunc2() {
  this.authForm.reset();
}

Here is a StackBlitz example

(顺便说一句,您可能还想重命名该函数...)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50564540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档