Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection Angular2,如何在ES6组件上使用@Host_Dependency Injection_Angular - Fatal编程技术网

Dependency injection Angular2,如何在ES6组件上使用@Host

Dependency injection Angular2,如何在ES6组件上使用@Host,dependency-injection,angular,Dependency Injection,Angular,我没有看到一个关于如何在ES6中使用Complete what Host(将父引用注入子引用)的示例 关于如何实现这一点,有什么想法吗?有一个主机变量,可以与任何指令一起使用。不确定是否有@Host decorator。 另外,如果您必须注入一些服务,您可以在组件装饰器中使用提供者 @Directive({ selector: 'someDir' }) class SomeDir { @ContentChildren(ChildDirective) contentChildre

我没有看到一个关于如何在ES6中使用Complete what Host(将父引用注入子引用)的示例


关于如何实现这一点,有什么想法吗?

有一个
主机
变量,可以与任何指令一起使用。不确定是否有
@Host decorator
。 另外,如果您必须注入一些服务,您可以在组件装饰器中使用
提供者

 @Directive({
   selector: 'someDir'
 })
 class SomeDir {
   @ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
   ngAfterContentInit() {
    // contentChildren is set
   }
 }
你可以查一下以供参考

对于主机实现,您可以查看实现

更新

你可以试试ContentChildren decorator

 @Directive({
   selector: 'someDir'
 })
 class SomeDir {
   @ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
   ngAfterContentInit() {
    // contentChildren is set
   }
 }
@指令({
选择器:“someDir”
})
类SomeDir{
@ContentChildren(ChildDirective)ContentChildren:QueryList;
ngAfterContentInit(){
//内容已设置
}
}

为了便于参考,您可以检查是否有一个
Host
变量,该变量可以与任何指令一起使用。不确定是否有
@Host decorator
。 另外,如果您必须注入一些服务,您可以在组件装饰器中使用
提供者

 @Directive({
   selector: 'someDir'
 })
 class SomeDir {
   @ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
   ngAfterContentInit() {
    // contentChildren is set
   }
 }
你可以查一下以供参考

对于主机实现,您可以查看实现

更新

你可以试试ContentChildren decorator

 @Directive({
   selector: 'someDir'
 })
 class SomeDir {
   @ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
   ngAfterContentInit() {
    // contentChildren is set
   }
 }
@指令({
选择器:“someDir”
})
类SomeDir{
@ContentChildren(ChildDirective)ContentChildren:QueryList;
ngAfterContentInit(){
//内容已设置
}
}

为了便于参考,您可以查看Thx。我不想在组件上使用指令来获取同一组件上的引用。我只是试图访问我的组件的父组件。我想让它知道什么时候孩子身上发生了什么。另外,我不能让父对象直接在模板上侦听子对象的事件,因为模板是由用户定义的(使用ng content).Thx。我不想在组件上使用指令来获取同一组件上的引用。我只是试图访问我的组件的父组件。我想让它知道什么时候孩子身上发生了什么。此外,我不能让父对象直接在模板上侦听子对象的事件,因为模板是由用户定义的(使用ng内容)。