2016-12-06 34 views
0

摘要结合的功能为无效的输入FormControl在角2

我建立与验证图案的FormControl。当模式无效时,我想调用一个函数,该函数将显示与正在抛出的错误相关的错误消息。当输入字段是无效的调用

ngOnInit() { 
     this.personalForm = new FormGroup({ 
      email : new FormControl(this.auth.user.email,Validators.required), 
      first_name: new FormControl(null,[ 
       Validators.required, 
       Validators.pattern("^[a-zA-Zñáéíóúü']{1,30}$") 
      ]), 
     }); 
    } 

功能。

showError(); 

问题

是否可以调用一个函数,如果错误被抛出,当输入字段无效检索错误?

回答

0

二者必选其一statusChanges(有效或无效)或valueChanges事件FormGroup要做到这一点:

this.personalForm.valueChanges.debounceTime(500).subscribe((data) => { 
    if (!this.personalForm.valid) { 
    this.showError(); 
    } 
} 
+0

是statusChanges实例或valueChanges在文档的某个地方我可以阅读有关?基本上,当用户离开输入字段时,如果出现错误,我想调用一个函数。 – wuno

+0

他们在这里https://angular.io/docs/ts/latest/api/forms/index/AbstractControl-class.html 要做的事情,当用户离开输入字段,你可能需要看看'模糊'事件(像这样的东西http://stackoverflow.com/questions/34918198/how-to-use-onblur-event-on-angular2) –

+0

嘿,我感谢你试图帮助我。但这真的不适合我。你能否把这件事稍微分开一点,这样我就明白了。自昨天起我一直在为此而苦苦挣扎。 – wuno