反应式表单验证 angular2 [英] Reactive form-validation-angular2
本文介绍了反应式表单验证 angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够从子组件获取值,但我需要验证父组件中是否存在这些值.
I am able to get the values from the child but I need to validate the values in my parent component that is present or not.
parent.html:
<browser (notify)="onselect($event)" [template]="coreActivity" (onselect)="onselect($event)" formControlName="template" ></browser>
parent.ts :
export class CreateCommunityComponent implements OnInit { userForm: FormGroup; public tagarray=[]; coreActivity = [
{value: 'Professional', viewValue: 'Professional', tool: 'Forum' },
{value: 'Travel', viewValue: 'Travel', tool: 'quora'},
{value: 'Arts', viewValue: 'Arts', tool: 'stackoverflow'},
{value: 'Technology', viewValue: 'Technology', tool: 'quora'},
{value: 'Business', viewValue: 'Business', tool: 'Forum'},
{value: 'Science', viewValue: 'Science', tool: 'quora'},
{value: 'Education', viewValue: 'Education', tool: 'quora'}
]; visibility = [
{value: 'Public', viewValue: 'Public'},
{value: 'Private', viewValue: 'Private'},
{value: 'Moderate', viewValue: 'Moderate'}
]; tags = [
{value: 'tag-one', viewValue: 'tagone'},
{value: 'tag-two', viewValue: 'tagtwo' },
{value: 'tag-three', viewValue: 'tagthree'}
];
constructor(private dialog: MdDialog, private fb: FormBuilder,private router: Router) { this.createForm(); } createForm() {
this.userForm = this.fb.group({
domainName: ['', [Validators.required, Validators.pattern('[a-z.]{8,20}')]],
communityName: ['', Validators.required],
Purpose: ['', Validators.required],
visibility: ['Public', Validators.required],
template: ['',Validators.required],
tagSelection: ['', Validators.required],
termscondition: ['', Validators.required]
});
}
// check whether the card is clickable or not
onselect(selectedTemplate: any)
{
console.log(selectedTemplate);
return selectedTemplate;
}
在我验证子值是否存在时出错:
It is giving error while I am validating the child value is present or not:
错误错误:没有名称的表单控件的值访问器:'模板'
ERROR Error: No value accessor for form control with name: 'template'
推荐答案
您需要将 ngDefaultControl
添加到您的
标签.
You need to add ngDefaultControl
to your <browser>
tag.
<browser ngDefaultControl (notify)="onselect($event)"
[template]="coreActivity" (onselect)="onselect($event)"
formControlName="template" ></browser>
希望这对你有用.
这篇关于反应式表单验证 angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文