[英]how to create a hyperlink for values from an array in Angular 8
I have data that looks something like this我有看起来像这样的数据
plans:[
{
"planName": "PlanA",
"planCode": "PLN001"
},
{
"planName": "PlanB",
"planCode": "PLN002"
},
{
"planName": "PlanC",
"planCode": "PLN003"
}
]
I need to display in UI HTML like below我需要在 UI HTML 中显示,如下所示
<p> User 1 is enrolled in PlanA , PlanB , PLanC </p>
question1: How to get the values from the plans array to display like above with commas in one statement.问题1:如何从计划数组中获取值以在一个语句中用逗号显示如上。
question2: I need to give a hyperlink to each plan to the question1 statement which will take to a different component based on the plan code.问题 2:我需要为问题 1 语句的每个计划提供一个超链接,该语句将根据计划代码转到不同的组件。
<a href="#{planCode}_details">{{PlanName}}</a>
The other components that the hyperlink has to take have an id to it超链接必须采用的其他组件具有它的 id
id="{{p.planCode}}_details"
Any help is appreciated任何帮助表示赞赏
This is what I tried so far.这是我到目前为止所尝试的。
<p *ngIf="plans?.length > 0">{{UserName}} has a balance in the {{plans.planName.join(', ')}} .</p>
var plans = [ { "planName": "PlanA", "planCode": "PLN001" }, { "planName": "PlanB", "planCode": "PLN002" }, { "planName": "PlanC", "planCode": "PLN003" }] var result = (plans.map(plan => plan.planName)).reduce((acc, curr) => acc + ', ' + curr) console.log(result);
planName
properties.
planName
属性的逗号分隔字符串。
planNames: string; this.planNames = (plans.map(plan => plan.planName)).reduce((acc, curr) => acc + ', ' + curr);
And use it the template并使用它的模板
<p> User 1 is enrolled in {{ planNames }} </p>
You could bind member variables to the href
attribute using either data binding notation or interpolation.您可以使用数据绑定表示法或插值将成员变量绑定到
href
属性。
<ng-container *ngFor="let plan of plans"> <a href="#{{ plan?.planCode }}_details">{{ plan?.PlanName }}</a> </ng-container>
I am not sure what you're exactly after, but to combine both the questions, you could do something like following我不确定您到底在追求什么,但是要将这两个问题结合起来,您可以执行以下操作
<p>User 1 is enrolled in <ng-container *ngFor="let plan of plans; let last=last"> <a href="#{{ plan?.planCode }}_details">{{ plan?.PlanName }}</a> <ng-container *ngIf=",last">; </ng-container> </ng-container> </p>
In this case you do not need the additional variable planNames
.在这种情况下,您不需要额外的变量
planNames
。
Maybe this:也许是这样:
<p> User 1 is enrolled in
<a *ngFor="let plan of plans" href="#{{plan.planCode}}_details">{{plan.PlanName}}</a>
</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.