Javascript angular2-获取外部组件中内部组件的DOM元素';谁的控制器?
例如: outer.htmlJavascript angular2-获取外部组件中内部组件的DOM元素';谁的控制器?,javascript,html,angular,Javascript,Html,Angular,例如: outer.html inner-c.html 福 如何访问outer.component.tscontroller中的#barDOM元素列表?可能吗 我试过了 @ContentChildren('bar')输入元素:QueryList和@ViewChildren('bar')inputElems:QueryList 在outer.component.ts中,但是这些返回一个空列表。我不相信您可以直接从其他组件访问元素。但是,您可以在子组件中设置事件发射器,以发射子元素的Elem
inner-c.html
福
如何访问outer.component.ts
controller中的#bar
DOM元素列表?可能吗
我试过了
@ContentChildren('bar')输入元素:QueryList
和@ViewChildren('bar')inputElems:QueryList代码>
在outer.component.ts
中,但是这些返回一个空列表。我不相信您可以直接从其他组件访问元素。但是,您可以在子组件中设置事件发射器,以发射子元素的ElementRef
。试试下面的方法
内-c.component.ts
从'@angular/core'导入{Component,ViewChild,ElementRef,Output,EventEmitter};
@组成部分({
选择器:“内部-c”,
templateUrl:'./inner-c.component.html',
样式URL:['./inner-c.component.css']
})
导出类innercomponent{
@Output()childElement=neweventemitter();
@ViewChild('bar')
设置viewChild(列表:ElementRef){
this.childElement.emit(列表);
}
构造函数(){
}
}
inner-c.component.html
福
app.component.ts
导出类AppComponent{
y=[0,1,2,3,4];
子元素(事件){
console.log(事件);
}
}
app.component.html
<div *ngFor="let x of y; let i = index">
<inner-c></inner-c>
</div>
<div #bar>
<div class="row no-gutters mt-3 mb-3 ml-5 mr-5">
foo
</div>
</div>