gpt4 book ai didi

javascript - ngFor 项目 Angular 8 的动态 routerLink 值

转载 作者:行者123 更新时间:2023-12-03 23:50:23 24 4
gpt4 key购买 nike

我正在尝试使用 [routerLink] 从 NgFor 路由动态值。

<li class="list-group-item d-flex justify-content-between align-items-center"
*ngFor="let factors of factorsList">
<span>{{factors | titlecase}}</span>
<a [routerLink]="'../factors'" class="btn btn-primary btn-sm">Reset <span class="sr-only">{{factors}}</span></a>
</li>

该代码应生成如下等效代码
<li class="list-group-item d-flex justify-content-between align-items-center">
Google
<a [routerLink]="['../google']" class="btn btn-primary btn-sm">Reset <span class="sr-only">google</span></a>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
YubiKey
<a [routerLink]="['../yubikey']" class="btn btn-primary btn-sm">Reset <span class="sr-only">yubi
key</span></a>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Backup codes
<a [routerLink]="['../backupcode']" class="btn btn-primary btn-sm">Reset</a>
</li>

但是由于某些原因, [routerLink]="'../factors'" 中的值(value)因子没有被替换。

组件类
export class MfaResetComponent implements OnInit {
mfaApp = MfaApp
public factorsList: string[] = [];
constructor(private genericHttpClientService: GenericHttpClientService) { }

ngOnInit() {
this.getResetFactors();
}
getResetFactors() {
this.genericHttpClientService.GenericHttpGet<FactorsViewModel[]>(`url`).subscribe(item => {
this.factorsList = [...new Set(item.filter(factor => factor.status == MultiFactorAuthenticationEnrolmentStatus.Enrolled).map(factor => factor.provider))];
})
}
}

列表因素将是
this.factorsList = ['Google','okta','Yubikey','BackupCode']

我发现堆栈溢出的一些问题
Dynamic routerLink value from ngFor item giving error "Got interpolation ({{}}) where expression was expected"

Angular 2 dynamically set routerLink using a component property

最佳答案

尝试使用 [routerLink]="'../'+factors"routerLink="{{'../'+factors}}"
Working Demo

.ts

factorsList = ["about","services"]

.html
<li class="list-group-item d-flex justify-content-between align-items-center" *ngFor="let factors of factorsList">
<span>{{factors | titlecase}}</span>
<a [routerLink]="'../'+factors" class="btn btn-primary btn-sm">Reset <span class="sr-only">{{factors}}</span></a>
</li>

关于javascript - ngFor 项目 Angular 8 的动态 routerLink 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58831129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com