I am trying to route the dynamic value from the NgFor using [routerLink].
<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>
The code should generate the equivalent code as below
<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>
But for some reasons, the value factors are not replaced in [routerLink]="'../factors'"
Component 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))];
})
}
}
The list factor will be
this.factorsList = ['Google','okta','Yubikey','BackupCode']
Some of the question I found I stack overflow Dynamic routerLink value from ngFor item giving error "Got interpolation ({{}}) where expression was expected"
Angular 2 dynamically set routerLink using a ponent property
I am trying to route the dynamic value from the NgFor using [routerLink].
<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>
The code should generate the equivalent code as below
<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>
But for some reasons, the value factors are not replaced in [routerLink]="'../factors'"
Component 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))];
})
}
}
The list factor will be
this.factorsList = ['Google','okta','Yubikey','BackupCode']
Some of the question I found I stack overflow Dynamic routerLink value from ngFor item giving error "Got interpolation ({{}}) where expression was expected"
Angular 2 dynamically set routerLink using a ponent property
Share Improve this question edited Nov 13, 2019 at 6:07 San Jaisy asked Nov 13, 2019 at 6:00 San JaisySan Jaisy 17.2k43 gold badges206 silver badges348 bronze badges 2-
Show
factorsList
– Adrita Sharma Commented Nov 13, 2019 at 6:03 -
1
replace
[routerLink]="'../factors'"
with[routerLink]="'../'+factors"
– Vikram Singh Commented Nov 13, 2019 at 6:06
2 Answers
Reset to default 3Try using [routerLink]="'../'+factors"
or 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>
You are using string in [routerLink]="'../factors'"
to make it work
replace [routerLink]="'../factors'"
with [routerLink]="['../'+factors]"