2017-06-20 61 views
0

我能夠從孩子獲取值,但我需要驗證我的父組件中存在或不存在的值。Reactive form-validation-angular2

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

這是給錯誤,而我確認孩子值存在與否:

ERROR Error: No value accessor for form control with name: 'template'

+0

請提供一些html內容 –

+0

您是否導入了'FormsModule'和'ReactiveFormsModule'? –

+0

你能爲孩子添加模板嗎? – Alex

回答

0

您需要添加ngDefaultControl添加到您的<browser>標籤。

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

希望這會對你有用。

+0

not working .. :( –

+0

我使用formgroup來驗證我的表單字段,我需要使用formcontrol驗證模板字段 –

+0

您可以刪除並嘗試嗎? –