[英]how to get first element in angular 5 using ngFor directive?
ngFor指令在我过去用来显示颜色数组中的所有颜色时工作正常,但是当我尝试检查它是否是数组的第一个元素时,它显示错误“无法绑定到ngForFirst,因为它不是。” ... 等等等等等等 ”。 它几乎显示了一个充满错误的页面。 我对angular非常陌生,我正在通过辅导老师学习的教程没有任何错误。 不知道为什么。 帮我
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-test',
template: `
<div *ngFor = "let color of colors; first as i">
<h3> {{i}} </h3>
</div>` ,
styles: []
})
export class TestComponent implements OnInit {
public name = "Dipesh";
public colors = ["Red", "Black", "Green"];
constructor() { }
ngOnInit() {
}
}
尝试这个
<div *ngFor = "let color of colors; let first = first; let last = last">
<h3> {{first}} </h3>
<h3> {{last}} </h3>
</div>
您可以像这样访问第一个元素:
<div *ngFor="let color of colors; let first = first;"> ...
注意:
您可以使用更多功能,例如: 奇数 , 偶数或末尾
使用“ as”,您尝试将“ first ”转换为“ i ”类型的对象。 这就是为什么您会出错。
您可以使用“ let”关键字以角度声明变量。 例如:让variableName =索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.