string concatenation with property binding in angular2

时间:2016-04-07 10:26:46

标签: javascript angular2-template

in Angular 2 we have several way to create property bindings in templates. I could do something like this:

<li *ngFor="#course of courses; #i = index" id="someselector-{{i}}">{{course}}</li>

Is it possible to obtain the same result using the square brakets syntax?

<li *ngFor="#course of courses; #i = index" [id]="someselector-i">{{course}}</li>
                                            ^^^^^^^
                                  how create string concatenation?

Thanks, G.

1 个答案:

答案 0 :(得分:41)

我发现你可以使用方括号来使用这种语法:

<li *ngFor="#course of courses; #i = index" [id]="'someselector-'+i">{{course}}</li>

欲了解更多信息,请查看Pascal Precht的这篇有趣文章:http://blog.thoughtram.io/angular/2015/08/11/angular-2-template-syntax-demystified-part-1.html