我尝试使用包含控件表单函数的单独formControle文件依赖项来测试ReactiveFormsControle。我需要使用一个observable传递表单控件并在此之后返回消息错误。但是当我试图测试我有这个错误时: TypeError:无法读取属性' map'未定义的
任何人都可以帮助我。
这是我的代码:
这是如何调用表单控件功能(进程消息):
ngAfterViewInit(): void {
// Watch for the blur event from any input element on the form.
let controlBlurs: Observable<any>[] = this.formInputElements
.map((formControl: ElementRef) => Observable.fromEvent(formControl.nativeElement, 'blur'));
// Merge the blur event observable with the valueChanges observable
Observable.merge(this.signUpForm.valueChanges, ...controlBlurs).debounceTime(1000).subscribe(value => {
this.displayeErrorMessage = this.formValidator.processMessages(this.signUpForm);
});
}
processMessage函数:
processMessages(container : FormGroup) : {[key : string]: string}{
let messages = {};
for(let controlKey in container.controls){
if(container.controls.hasOwnProperty(controlKey)){
let c = container.controls[controlKey];
if(c instanceof FormGroup){
let childMessages = this.processMessages(c);
Object.assign(messages, childMessages);
}else {
if(this.validationErrorMessages[controlKey]){
messages[controlKey]='';
if((c.dirty || c.touched) && c.errors){
Object.keys(c.errors).map(messageKey =>{
if(this.validationErrorMessages[controlKey][messageKey]){
messages[controlKey] += this.validationErrorMessages[controlKey][messageKey] + ' '
}
})
}
}
}
}
}
return messages;
}
*这是处理错误的测试代码*
beforeEach(() => {
fixture = TestBed.createComponent(SignUpComponent);
component = fixture.componentInstance;
debugHelpblck = fixture.debugElement.queryAll(By.css('.help-block'));
component.ngOnInit();
component.ngAfterViewInit(); // this handle the error
fixture.detectChanges();
});
答案 0 :(得分:-1)
在filter(Boolean)
之前添加map()
,或者对您的数据更加谨慎(;
不确定您的错误出现在哪里,如果这是以下所有代码:
let controlBlurs: Observable<any>[] = this.formInputElements
.filter(Boolean)
.map(...)
或
Object.keys(c.errors).filter(Boolean).map(messageKey =>{...})
应修正错误......