2017-06-20 58 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

您好可以請你提供更多細節?如何驗證