We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<div *ngFor="let book of books; let i=index; let isFirst=first; let isLast=last; let isEven=even; let isOdd=odd; trackBy: trackByFunction" > </div>
let book of books
let i=index
first
last
even
odd
trackByFunction是一个函数,它的第一个参数是当前的index,第二个是当前的项,它需要返回一个数据,angular看它是否改变来判断这一项是否应该被重新渲染(提升性能)。
trackByFunction(index, book){ return book.id; }
上面表示如果id改变了则这一项会重新渲染。
let i=index可以写成index as i
index as i
<div *ngFor="let book of books; index as i; first as isFirst; last as isLast; even as isEven; odd as isOdd;" > </div>
或者
<ng-template ngFor let-book [ngForOf]="books" let-i="index"> {{ book }} </ng-template>
但是这种写法只能在ng-template上写。
ng-template
The text was updated successfully, but these errors were encountered:
No branches or pull requests
*ngFor使用方法
简要写法
let book of books
: 循环books,循环的每一项是booklet i=index
:index是当前下标,赋值给 i, 下面的形式相同。first
:first:是否为第一项,赋值给isFirstlast
: 是否为最后一项even
: 是否为偶数项odd
: 是否为奇数项trackByFunction是一个函数,它的第一个参数是当前的index,第二个是当前的项,它需要返回一个数据,angular看它是否改变来判断这一项是否应该被重新渲染(提升性能)。
上面表示如果id改变了则这一项会重新渲染。
还可以这样写
let i=index
可以写成index as i
或者
但是这种写法只能在
ng-template
上写。The text was updated successfully, but these errors were encountered: