Angular 反应形式-验证-角度2

Angular 反应形式-验证-角度2,angular,angular2-forms,Angular,Angular2 Forms,我可以从子组件获取值,但我需要验证父组件中是否存在的值 parent.html: <browser (notify)="onselect($event)" [template]="coreActivity" (onselect)="onselect($event)" formControlName="template" ></browser> export class CreateCommunityComponent implements OnInit { user

我可以从子组件获取值,但我需要验证父组件中是否存在的值

parent.html:

<browser (notify)="onselect($event)"  [template]="coreActivity" (onselect)="onselect($event)" formControlName="template" ></browser>
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;
}
在验证子值是否存在时出现错误:

错误:名为“模板”的表单控件没有值访问器


您需要将
ngDefaultControl
添加到
标记中

<browser ngDefaultControl (notify)="onselect($event)"  
  [template]="coreActivity" (onselect)="onselect($event)" 
formControlName="template" ></browser>


希望这对您有用。

我正在使用formgroup来验证我的表单字段,我需要使用FormControls来验证模板字段。您可以删除并尝试吗?获取错误:错误:没有名为“template”的表单控件的值访问器。当您删除
formGorup?时,您会遇到相同的错误。
请提供还有一些html内容导入了
FormsModule
ReactiveFormsModule
?您是否也可以为子级添加模板?