如何傾聽來自孩子組件內部子模板的事件時,他們是從父模板叫什麼名字?兒童的意見和事件偵聽器在Angular2
我有2個組件:app.ts和child.ts;
app.ts:
@Component({
selector: 'app',
template: '<button>Main button</buton><router-outlet></router-outlet>',
encapsulation: ViewEncapsulation.None,
directives: [ROUTER_DIRECTIVES],
})
@RouteConfig([
{path: '/child/', component: ChildCmp, as: 'Child'}
])
export class AppCmp {
}
child.ts:
@Component({
selector: 'child',
template: 'This is child page!'
})
export class AppCmp {
// This method should be called, when mail button is pressed
onChange() {
}
}
我不想點擊事件添加到按鈕,而不是我想補充某種監聽子組件中的onChange函數,以便在父視圖中的按鈕被單擊時監聽。我用HostListener嘗試過,但只有當HostListener添加到父組件時纔會觸發。
我不知道我理解你的問題。你真的想讓ChildCmp聽AppCmp中的按鈕嗎?這將違反angular2的Component結構範例。組件不應該有可能的父組件的知識。爲什麼不聽AppCmp(父代)中的按鈕點擊事件,然後在客戶端調用函數onChange()? –
正確的,我做的是我在頁面頂部有語言選擇(這是父組件),我想,當用戶更改語言重新加載子組件的內容的原因。 – Hurley