Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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 fidn html信息,带角度指令_Angular_Typescript_Directive - Fatal编程技术网

Angular fidn html信息,带角度指令

Angular fidn html信息,带角度指令,angular,typescript,directive,Angular,Typescript,Directive,我想用指令查找元素信息。 我把这个指令放在标签上,我需要获得元素信息,如宽度和顶部和 现在我在指令中编写以下代码: loadingFactory: ComponentFactory<LoadingComponent>; loadingComponent: ComponentRef<LoadingComponent>; @Input() set SnipperLoading(data: Promise<any>) { // this.

我想用指令查找元素信息。 我把这个指令放在标签上,我需要获得元素信息,如
宽度
顶部

现在我在指令中编写以下代码:

     loadingFactory: ComponentFactory<LoadingComponent>;
  loadingComponent: ComponentRef<LoadingComponent>;

  @Input() set SnipperLoading(data: Promise<any>) {
    // this.containerRef.clear();
    this.containerRef.createEmbeddedView(this.templateRef);
    this.loadingComponent = this.containerRef.createComponent(this.loadingFactory);
    data.then(() => {
      this.containerRef.clear();
      this.containerRef.createEmbeddedView(this.templateRef);
    });
  }

  constructor(
    private el: ElementRef,
    private containerRef: ViewContainerRef,
    private templateRef: TemplateRef<any>,
    private componentFactoryResolver: ComponentFactoryResolver
  ) {
    this.loadingFactory = this.componentFactoryResolver.resolveComponentFactory(LoadingComponent);
    let myCurrentContent: string = el.nativeElement.innerHTML; // get the content of your element

    el.nativeElement.innerHTML = 'my new content'; // set content of your element
  }

.如何访问angular directive中的元素信息?

您的打字是故意的吗?(
width
vs
with
)@PhilippMeissner无人。我修正了我实际上指的是你提供的代码。我建议不要把逻辑放在指令的构造函数中。请改用Angular的生命周期挂钩,不要忘记,在尝试访问视图中的元素之前,可能需要使用ngAfterViewInit来确保视图存在。
      <label *SnipperLoading="user$"> SnipperLoading </label>
  console.log(el.nativeElement.with)