1
我有一個父組件和一個子組件。我希望能夠從父組件上的某個方法調用子組件上的某個子方法。 有沒有辦法獲得對父類的子組件實例的引用並調用子公共方法?對子組件執行方法
@Component({
selector: 'child-component'
})
@View({
template: `<div>child</div>`
})
class ChildComponent{
constructor() {
}
doChildEvent() {
// some child event
}
}
@Component({
selector: 'parent-component'
})
@View({
template: `
<child-component #child></child-component>
`,
directives: [
ChildComponent
]
})
class ParentComponent {
private child:ChildComponent;
constructor() {
}
onSomeParentEvent() {
this.child.doChildEvent();
}
}
我試着把一個哈希放在模板中的孩子,並在類中引用它,但沒有成功。
'#child' - 這些本地模板變量不會成爲組件上的屬性(如您發現的那樣)。我在關於它們範圍的文檔中找不到任何東西(我不想看源代碼)。看來它們的範圍僅限於模板。 –