要将数组中的最后一项变为链接,可以使用以下方法:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
`,
})
export class AppComponent {
links = ['Link 1', 'Link 2', 'Link 3'];
lastLink = '';
constructor() {
this.lastLink = this.links[this.links.length - 1];
}
}
*ngFor
指令循环遍历数组,并使用let last = last
语法来判断是否为最后一项。*ngIf
指令在模板中动态地显示不同的链接。如果不是最后一项,显示普通链接;如果是最后一项,显示特殊链接。lastLink
。这样,就可以将数组中的最后一项变为链接了。