Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Javascript 如何在Angular2中的列表的第一项上设置模板变量引用?_Javascript_Angular_Ngfor - Fatal编程技术网

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];
  }
}