Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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
Angular 使用父级上的指令获取嵌套元素_Angular_Angular Directive - Fatal编程技术网

Angular 使用父级上的指令获取嵌套元素

Angular 使用父级上的指令获取嵌套元素,angular,angular-directive,Angular,Angular Directive,这是我的代码 <parent my-directive [toHide]="childRef"> <child bottom right #childRef> <button>Some text </button> </child > </parent > 正如您所看到的,我正在使用this.renderer设置元素的样式,但是我得到了以下结果 export class MyDirective { @I

这是我的代码

<parent my-directive [toHide]="childRef">

 <child bottom right #childRef>
    <button>Some text </button>
 </child >

</parent >
正如您所看到的,我正在使用
this.renderer
设置元素的样式,但是我得到了以下结果

export class MyDirective {
  @Input("toHide") localRef;

  constructor(public element:ElementRef,public renderer:Renderer) {
    console.log('Hello MyDirective Directive');
  }

  ngAfterViewInit() {
    console.log("All Transtition set");
    console.log(this.localRef);
this.renderer.setElementStyle(this.localRef, 'webkitTransition', 'transform 500ms,top 500ms');
  }
错误错误:未捕获(承诺中):TypeError:el.style未定义

这方面的任何帮助都是值得的。

如果
是一个普通的HTML元素(没有组件),那么它应该可以工作(添加了
nativeElement

this.renderer.setElementStyle(
this.localRef.nativeElement,“WebKittTransition”,“transform 500ms,top 500ms”);
如果
是一个角度分量,请更改此行

@Input(“toHide”)localRef;

@ContentChild('childRef',{read:ElementRef})localRef;
并删除[toHide]=“childRef”

如果元素包含模板变量 是一个普通HTML元素,读取引用将返回一个
ElementRef
,可以使用
nativeElement
属性访问实际元素

如果它是一个组件或承载一个指令,那么读取引用将返回无法用于访问DOM元素的组件或指令实例

@ViewChild(ren)
@ContentChild(ren)
允许使用
read
参数指定从模板变量引用返回的内容,但从模板内访问模板变量引用并不提供这一点

但是我建议使用

@HostBinding('class.is transition')
isTransition:布尔值为false;
在指令中使用CSS,如

父[主机指令]>子{
-webkit转换:转换500毫秒,顶部500毫秒;
}
应用样式。

如果
是纯HTML元素(无组件),则这应该可以工作(添加了
nativeElement

this.renderer.setElementStyle(
this.localRef.nativeElement,“WebKittTransition”,“transform 500ms,top 500ms”);
如果
是一个角度分量,请更改此行

@Input(“toHide”)localRef;

@ContentChild('childRef',{read:ElementRef})localRef;
并删除[toHide]=“childRef”

如果元素包含模板变量 是一个普通HTML元素,读取引用将返回一个
ElementRef
,可以使用
nativeElement
属性访问实际元素

如果它是一个组件或承载一个指令,那么读取引用将返回无法用于访问DOM元素的组件或指令实例

@ViewChild(ren)
@ContentChild(ren)
允许使用
read
参数指定从模板变量引用返回的内容,但从模板内访问模板变量引用并不提供这一点

但是我建议使用

@HostBinding('class.is transition')
isTransition:布尔值为false;
在指令中使用CSS,如

父[主机指令]>子{
-webkit转换:转换500毫秒,顶部500毫秒;
}

应用样式。

控制台.log(this.localRef)的输出是什么?我怀疑是
child
指令实例将样式应用于nativeElement吗?
console.log(this.localRef)的输出是什么?我怀疑这是
child
指令实例您是否将样式应用于nativeElement?获取包含ContentChild的元素与此有何区别。element.nativeElement.getElementsByClassName?它使性能变得重要吗?不,你也可以这样做。它只是与SSR和web worker功能不兼容。有更多的细节。除此之外,我不知道任何具体的来源。可能会提供一些东西,但我不知道具体显示获取ContentChild元素与此不同。element.nativeElement.getElementsByClassName?它使性能变得重要吗?不,你也可以这样做。它只是与SSR和web worker功能不兼容。有更多的细节。除此之外,我不知道任何具体的来源。可能会提供一些东西,但我不知道具体细节