简体   繁体   English

如何在单插值angular2中插入多个表达式

[英]How to insert multiple expression in single interpolation angular2

How to insert multiple expression in single interpolation in angular 2, for example like this: 如何在角度2的单插值中插入多个表达式,例如:

{{ title + !isLast ? ' > ' : ''}}

Do you have any solution for this? 您对此有什么解决方案吗?

This particular case could be achieved with CSS after (and with last and :not() ) 这个特殊的情况可以在CSS after (以及last:not() )实现。

If you really want nested ternary expressions, just use () where approperiate. 如果您确实想要嵌套三元表达式,则在适当的地方使用()

Consider using a get xxx(): string {} getter in your TypeScript so the logic remains more readable. 考虑在TypeScript中使用get xxx(): string {} getter,以使逻辑保持可读性。

Finally, don't forget about npm packages like angular-translate which may make things simpler if you want to display text. 最后,别忘了使用诸如angular-translate之类的npm软件包,如果要显示文本,它们可能会使事情变得更简单。

The best solution for your requirement will be - 满足您需求的最佳解决方案是-

<span *ngFor="let item of listOne; let isLast = last">
          {{getItem(item, isLast)}}
</span>

in your template and implement getter in your component 在模板中并在组件中实现getter

getItem(item: string, isLast: boolean) {
    return `${item} ${isLast ? '>' : ''}`;
  }

Basically when you have complex manipulations use getter to get value after manipulations 基本上,当您进行复杂的操作时,请使用getter在操作后获取价值

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

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