1
我沒有看到關於如何使用完成的主機不(母公司注入到參考子)在ES6的例子。Angular2,如何使用@host上ES6組件
@Component({ ... })
class PlayButton {
constructor(@Host() playerService: PlayerService) {
}
}
有關我們如何實現這一點的任何想法?
我沒有看到關於如何使用完成的主機不(母公司注入到參考子)在ES6的例子。Angular2,如何使用@host上ES6組件
@Component({ ... })
class PlayButton {
constructor(@Host() playerService: PlayerService) {
}
}
有關我們如何實現這一點的任何想法?
有一個Host
變量,它可以與任何指令一起使用。不確定是否有@Host decorator
。 此外,如果有注入一些服務則可以在組件裝飾用providers
。
你可以看看Angular API以供參考。
對於主機實現你可以看看Attribute Directives實施。
更新
,你可以嘗試ContentChildren裝飾。
@Directive({
selector: 'someDir'
})
class SomeDir {
@ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
ngAfterContentInit() {
// contentChildren is set
}
}
僅供參考,你可以檢查Angular API
希望這有助於。
THX。我不希望使用組件上的指令來獲取相同組件的引用。我只是試圖訪問我的組件的父組件。 我要讓它知道什麼事情在孩子發生。此外,由於模板是由用戶定義的(使用ng-content),所以我無法讓父母直接在模板上監聽兒童的事件。 – Emanuel
@Emanuel:答案已經更新,你可以嘗試一下兒童內容。 –