javascript - 从 Angular 2+ 中的同级组件访问元素

标签 javascript angular dom angular-components

我有一组可以协同工作的组件。基本上,我显示一个标签列表,以及每个标签的数据列表。由于标签 Pane 的大小是可调整的,因此它成为数据组件的同级组件。即-我的列表组件如下所示:

<app-list-labels></app-list-labels>
<app-list-data></app-list-data>

列表标签和列表数据分别如下所示:

// app-list-labels:
<div class="label-header">Labels</div>
<div class="label-labels" id="labels-labels">
   <!-- all of my labels looped over and displayed -->
</div>

// app-list-data:
<div class="data-header">Labels</div>
<div class="data-data" id="data-data">
   <!-- all of my data rows looped over and displayed -->
</div>

labels-labelsdata-data都将overflow-y设置为auto,这样,如果行数超过容器大小,它们就可以滚动。两者之间的行数和大小始终相同。我的目标是如果一个容器正在滚动,则两个容器都可以滚动。因此,我需要将 (scroll) 事件监听器附加到这两个 div(#data-data#labels-labels) ,并更新非滚动元素的滚动值。我的问题是 - 如何从同级组件中的一个组件访问元素?如果 app-labels 嵌入到 app-data 中,那就很简单了,但作为 sibling ,我不知道该怎么做。

最佳答案

您可以尝试使用 @Output 装饰来公开 Div,如下所示:

<app-component-one (divComponent)="divOne = $event"></app-component-one>
<app-component-two (divComponent)="divTwo = $event"></app-component-two>

sibling 1:

import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core';

@Component({
  selector: 'app-component-one',
  templateUrl: './component-one.component.html',
  styleUrls: ['./component-one.component.css']
})
export class ComponentOneComponent implements OnInit, AfterViewInit {

  @ViewChild('divComponent1') divComponent1: ElementRef;
  @Output() divComponent = new EventEmitter();

  constructor() {
  }

  ngOnInit() {
  }

  ngAfterViewInit(): void {
    this.divComponent.emit(this.divComponent1);
  }


}

sibling 2:

import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core';

@Component({
  selector: 'app-component-two',
  templateUrl: './component-two.component.html',
  styleUrls: ['./component-two.component.css']
})
export class ComponentTwoComponent implements OnInit, AfterViewInit {

  @ViewChild('divComponent2') divComponent1: ElementRef;
  @Output() divComponent = new EventEmitter();

  constructor() {
  }

  ngOnInit() {
  }

  ngAfterViewInit(): void {
    this.divComponent.emit(this.divComponent1);
  }

}

父组件,具有兄弟组件的组件:

import { AfterViewInit, Component, ElementRef, OnInit } from '@angular/core';

@Component({
  selector: 'app-component-base',
  templateUrl: './component-base.component.html',
  styleUrls: ['./component-base.component.css']
})
export class ComponentBaseComponent implements OnInit, AfterViewInit {

  divOne: ElementRef;
  divTwo: ElementRef;

  constructor() {
  }

  ngOnInit() {
  }

  ngAfterViewInit(): void {
    console.log('div one' , this.divOne);
    console.log('div two' , this.divTwo);
  }


}

关于javascript - 从 Angular 2+ 中的同级组件访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285863/

相关文章:

javascript - javascript中如何调用动态函数

javascript - 在 redux 的 Provider 中包装多个 React DOM 组件

javascript - 在异步读取文件的同时解析 JSON

javascript - 如何轻松解析JSON?

unit-testing - Angular 2 单元测试 : Custom Pipe error The pipe could not be found

angular - 使用 jasmine 测试 NGXS 异常

javascript - 使用 Vanilla javascript 将元素高度设置为等于宽度

angular - 没有导出成员 HttpClientModule、BrowserAnimationsModule

JavaScript:DOM 加载事件、执行顺序和 $(document).ready()

javascript - 对于 body 中的每个元素类型