Javascript 如何在Angular2中的列表的第一项上设置模板变量引用?
我想在列表的第一项上设置一个模板变量引用。这是我的代码(不起作用):Javascript 如何在Angular2中的列表的第一项上设置模板变量引用?,javascript,angular,ngfor,Javascript,Angular,Ngfor,我想在列表的第一项上设置一个模板变量引用。这是我的代码(不起作用): 有人知道如何做到这一点吗?没有办法有条件地添加或不添加模板变量 你能做的就是使用 @ViewChilderen('firstitem') items:QueryList<ElementRef>; firstItem:ElementRef; ngAfterViewInit() { var items = this.items.toArray(); if(items.length) { this.fi
有人知道如何做到这一点吗?没有办法有条件地添加或不添加模板变量 你能做的就是使用
@ViewChilderen('firstitem') items:QueryList<ElementRef>;
firstItem:ElementRef;
ngAfterViewInit() {
var items = this.items.toArray();
if(items.length) {
this.firstItem = items[0];
}
}
@viewChildren('firstitem')项:QueryList;
第一项:ElementRef;
ngAfterViewInit(){
var items=this.items.toArray();
if(项目长度){
this.firstItem=项目[0];
}
}
然后可以使用firstItem
进行模板中的绑定
<ul>
<li #firstitem>
some content
</li>
<li>
other content
</li>
</ul
@ViewChilderen('firstitem') items:QueryList<ElementRef>;
firstItem:ElementRef;
ngAfterViewInit() {
var items = this.items.toArray();
if(items.length) {
this.firstItem = items[0];
}
}