反应式表格验证 - 角度2

时间:2017-06-20 06:22:02

标签: angular angular2-forms

我能够从孩子那里获取值,但我需要验证我父组件中是否存在的值。

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;
}

在验证子值是否存在时出错:

  

错误错误:没有名称为'template'的表单控件的值访问器

1 个答案:

答案 0 :(得分:0)

您需要将ngDefaultControl添加到<browser>标记中。

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

希望这对你有用。