angular2에서 폼 유효성 검사를 작성하고 있습니다. 내 오류 내 HTML 파일 형태 즉각도 2의 폼 유효성 확인
<form id="commentform" class="comment-form" novalidate
[ngFormModel] = "contact" (ngSubmit)="submit(contact.value)">
<div class="form-input">
<input type="text" id="author" name="author" placeholder="Name *" value=""
[ngFormControl] = "contact.controls['author']" pattern="[a-zA-Z ]*"/>
<div [hidden]="author.valid">Name is required
</div>
</div>
내가 div [hidden]="author.valid"
에서 오류가 발생하고이 들어 Cannot read property 'valid' of undefined
입니다. 저자는 변수가 아니기 때문에 오류가 내 구성 요소 파일
import {FormBuilder ,ControlGroup,Validators } from '@angular/common';
@Component({
selector: 'my-contact',
templateUrl : 'app/contact.html'
}
export class ContactComponent {
contact : ControlGroup;
constructor(private _OnChange:OnChange,private _formBuilder : FormBuilder){
this.ngAfterViewInit();
}
ngOnInit() : any{
this.contact = this._formBuilder.group({
'author' : ['',Validators.required] }); }
submit(){
console.log(JSON.stringify(this.contact.value));
}
왜? 차이점은 무엇입니까? 어떤 참조? – dakab
""에'# author = "ngForm"'을 추가하여 "[hidden] ="author.valid "'"Checked after Expression was changed "와 같은 오류를 발생시키는 작업을해야합니다. 대신에'author = new Control ('', [Validators.required]);'변수를 만들고 이것을 this._formBuilder.group ({ 'author': this.author})'에 추가하면, 바인딩도 잘 작동합니다. 또한 새 양식 모듈로 마이그레이션하는 것이 좋습니다. 이미 교체 된 것을 배우는 데 시간을 투자 할 필요가 없습니다. –