ContentChildren可以选择vanilla html组件吗?

时间:2018-01-22 15:23:29

标签: angular

我在模板中有一个带有普通<ng-content>选择器的Angular组件。

我正在尝试将一个ElementRef实例添加到所有作为内容子项传递的html <input />元素。

如何编写ContentChildren注释以获取所有输入。

我试过了:

@ContentChildren('input', { descendants: true }) _inputChildren: QueryList<any>;

无济于事。我想避免专门为选择这些儿童输入制定指令,但如果这是唯一的解决方案,我会这样做。

1 个答案:

答案 0 :(得分:0)

你可以使用NgZone和查询选择器,虽然我不确定这是一个好习惯:

inputs: any[];
constructor(private zone: ngZone) {}

ngAfterViewInit() {
  this.zone.run(() => {
    // spread operator to have an actual array and not an HTML collection
    this.inputs = [...document.querySelector('input')];
  });
}