如果我有一個在 REVERSE 中列出的當前陣列,只需在 html 中更改它,它正在遍歷索引:
arrayName.length - i - 1
我怎么能訪問元素的最后一個陣列???下面是我嘗試的陳述句行,它失敗了,因為我們正在遍歷索引(通過每次迭代獲取 console.log 值)。
arrayName.length - 1 === i
uj5u.com熱心網友回復:
我認為訪問 .html 中陣列中最后一個元素的最簡單方法是: *ngFor="let item of items; let last = last"
uj5u.com熱心網友回復:
你可以這樣做:
HTML
<div *ngFor="let item of reverseArray, let i = index">
{{ 'index: ' i }} {{ i === reverseArray.length -1 ? 'Last element of the reverse array: "' item '"' : '"' item '"' }}
</div>
TS:
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
arrayName = [ 11, 89, 23, 7, 98 ];
reverseArray = [...this.arrayName].reverse();
constructor() {
console.log(this.reverseArray);
}
}
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418223.html
標籤:
