Angular 2自定义验证程序运行两次

时间:2016-04-21 16:49:55

标签: validation angular

我跟踪剥离的代码以进行调试。 emailForCloudUserValidator在单个NgOnInit运行时运行两次,将两条消息记录到控制台。

ngOnInit(){
  this.contactControlGroup = this.formBuilder.group({
    email:[this.contact.email, this.emailForCloudUserValidator]
  });
  this.formControls = this.contactControlGroup.controls;
}

private emailForCloudUserValidator(control: Control) :  { [s: string]: boolean }{
  if(!control.value){
    console.log("email value is null");
  }
  return;
}

1 个答案:

答案 0 :(得分:2)

enableProdMode()之前调用bootstrap()应该修复它:

import {enableProdMode} from "angular2/core";

enableProdMode();

默认情况下,Angular2在devMode中运行,其中更改检测始终运行两次,以检查指示应用程序中存在错误的模型不稳定性(产生错误消息Expression 'xxxx' was changed after it was checked