2016-12-15 91 views
1

我有以下設置:如何訪問子元素標籤ID

<parent> 
    <a (click)="getChildId(childId)"> 
    <child somethingRequired></child> 
</parent> 

而子模板文件:

<template #childId></template> 

能否請您指導我會是怎樣得到的最好的方法#childId並將其傳遞給getChildId函數。

感謝

回答

0

簡單的答案是使用ViewChild API在ChildComponent#localVariableParentCompnent,如下所示,

DEMO:https://plnkr.co/edit/V8srD4WnlowP8UACNIpI?p=preview

孩子

<input #childId> 

export class child { 
    @ViewChild('childId') childId:ElementRef; 
} 

<a (click)="writeValue(has.childId)">GrabValue</a><br> 
         //<<===has variable has an ability to access variables and methods of child component 
<child #has></child>        
     //<<===has is a local variable which can access variables and methods of child component 



export class AppComponent { 
    source='images/angular.png'; 
    writeValue(value){ 
    console.log('parent writeValue'); 
    console.log(value.nativeElement.value); 
    } 
} 
+0

謝謝你,這工作。 我有點困惑,因爲我試圖填充自舉彈出窗口,如果