2017-06-20 63 views
1

在这里,我需要访问父类的子值。 但我无法得到它。如果我使用指令,它会显示错误。 有人可以帮我在父组件显示孩子的价值观和孩子怎么值可以在反应形式进行验证?angular2需要在父类访问子值

parent.html:

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

parent.ts:

onselect(select: any) 
{ 
    console.log(select.value); 
} 

child.html:

<md-grid-list cols="3" rowHeight="100px"> 
    <md-grid-tile *ngFor="let core of template" (click)="selectcore(core)"> 
     {{core.value}} 
    </md-grid-tile> 
</md-grid-list> 

child.ts:

@Component({ 
    selector: 'template-browser', 
    templateUrl: './template-browser.component.html', 
    styleUrls: ['./template-browser.component.css'] 
}) 
export class TemplateBrowserComponent implements OnInit { 
    @Input() template; 

    @Output() notify: EventEmitter<string> = new EventEmitter<string>(); 
    constructor(private community: CreateCommunityComponent) { } 
    selectcore(core: any) { 
     // alert(core.value); 
     this.notify.emit(core.value); 
    } 
} 

回答

0

当你逝去的价值观通过EventEmitter你需要与儿童组件宣言通过它在parent.html

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

而且在功能下parent.ts:

onSelect(select) 
{ 
    console.log(select.value); 
} 
+0

嘿谢谢@独裁者 –

+0

我能够访问孩子的价值成parent.How验证< onselect($ event)“>使用反应表单验证??????当我尝试进行验证时发生错误;错误:错误:名称为'模板'的表单控件没有值存取器 –

+0

您能详细说明吗?如果可能的话创建为 –

0

为了在父母访问child.html,您需要创建另一个组件,并在父母使用该组件名称标签。 像组件名称为“子” 保持<child></child>在父母

+0

您好可以请你提供更多细节?如何验证