最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using ng-if inside ng-bind Angular Js - Stack Overflow

programmeradmin0浏览0评论
<span ng-repeat="tag in tags">
   {{tag + "," }}
</span>

I need to remove , after the last element. I know ng-if="$last" can solve the problem. But, as I don't have any parent element for {{tag}} I can't use ng-if so, just need some work around.

<span ng-repeat="tag in tags">
   {{tag + "," }}
</span>

I need to remove , after the last element. I know ng-if="$last" can solve the problem. But, as I don't have any parent element for {{tag}} I can't use ng-if so, just need some work around.

Share Improve this question edited Oct 8, 2016 at 7:06 Shakir Ahamed 1,3083 gold badges16 silver badges40 bronze badges asked Oct 8, 2016 at 6:59 Atul SharmaAtul Sharma 10.8k10 gold badges41 silver badges69 bronze badges 2
  • 1 check this answer – abpatil Commented Oct 8, 2016 at 7:06
  • @abpatil charm !! You saved my day :) – Atul Sharma Commented Oct 8, 2016 at 7:08
Add a ment  | 

2 Answers 2

Reset to default 5

You should use a ternary together with () in order to prevent weird oute:

<span ng-repeat="tag in tags">
   {{tag + ($last ? "" : ",")}}
</span>

Use a ternary operator within the mustache like this:

<span ng-repeat="tag in tags">
   {{tag + $last ? "" : "," }}
</span>

Cheers!

EDIT: Had written down the answer in a hurry before- correcting the mistake above:

<span ng-repeat="tag in tags">
   {{tag}}{{$last ? "" : ","}}
</span>

or

<span ng-repeat="tag in tags">
   {{tag + ($last ? "" : ",")}}
</span>
发布评论

评论列表(0)

  1. 暂无评论