简体   繁体   English

* ngfor Angular 2中的字符串连接

[英]String concatenation within *ngfor Angular 2

I would like to join strings in multiline within *ngfor angular 2 directive. 我想在* ngfor angular 2指令内的多行中加入字符串。

var contacts = [str1, str2, str3];

<div *ngFor="let c of contacts;let last = last; let str = str + c;">
    <div *ngIf="last">
         {{str}}
    </div>
</div>

I know the above code is not valid. 我知道上面的代码无效。 But I want to know something like above is possible to achieve? 但是我想知道上面的事情是可以实现的吗?

Expected Output should be 预期输出应为

Str1 STR1

Str2 STR2

Str3 STR3

Note: I want to achieve something like below code in C# using a angular ngfor. 注意:我想使用有角的ngfor在C#中实现以下代码。

   var str = "";

   foreach(var item in items)
   {
     str = str + item.toString();
   }
   console.log(str);

update 更新

*ngFor can be used to calculate values depending on previous items. *ngFor可用于根据先前的项目计算值。 The best way would be to prepare the list in code and then just bind *ngFor to that resulting list. 最好的方法是用代码准备列表,然后将*ngFor绑定到该结果列表。

original 原版的

You can't use arbitrary expressions with let . 您不能将任意表达式与let Only context variables provided by *ngFor can be assigned. 只能分配*ngFor提供的上下文变量。

This way it should produce the results you want 这样,它应该产生想要的结果

<div *ngFor="let c of contacts;let last = last">
    <div *ngIf="last">
         {{str}}{{c}}
    </div>
</div>

Perhaps something as simple as join would suffice: 也许像join这样简单的事情就足够了:

untested 未经测试

var contacts = ['str1', 'str2', 'str3'];

<div [innerHtml]="contacts.join('')"></div>

You can simply output the two strings one after another: 您可以简单地一个接一个地输出两个字符串:

let contacts = [str1, str2, str3];

<div *ngFor="let c of contacts;let last = last;">
    <div *ngIf="last">
         {{str}}{{c}}
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM