ngFor 项目的动态 routerLink 值 Angular 8
Dynamic routerLink value for ngFor item Angular 8
我正在尝试使用 [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'"
中没有替换值因子
组件Class
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']
我发现的一些问题堆栈溢出
Angular 2 dynamically set routerLink using a component property
尝试使用 [routerLink]="'../'+factors"
或 routerLink="{{'../'+factors}}"
.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>
您正在使用 [routerLink]="'../factors'"
中的字符串使其工作
将[routerLink]="'../factors'"
替换为[routerLink]="['../'+factors]"
我正在尝试使用 [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'"
组件Class
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']
我发现的一些问题堆栈溢出
Angular 2 dynamically set routerLink using a component property
尝试使用 [routerLink]="'../'+factors"
或 routerLink="{{'../'+factors}}"
.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>
您正在使用 [routerLink]="'../factors'"
中的字符串使其工作
将[routerLink]="'../factors'"
替换为[routerLink]="['../'+factors]"