I got really disappointed with angular... I'm trying to do a quite simple presentation-layer stuff and I can see no good solution while browsing the web (and stack overflow as well).
The case is: I've got a list of elements that I will generate links to, all of them will be comma-separated in HTML. Let's say it's:
var list = [{id: 1, name: "ab"},{id:2, name: "cd"}, ...];
My link should look something like:
<a href="#/someLink/{{ el.id }}">{{ el.name }}</a>
Easy, isn't it? So, basing on other SO questions, I tried this:
<a ng-repeat="el in list" href="#/someLink/{{ el.id }}">{{ el.name }}</a>{{$last ? '' : ', '}}
which doesn't work, because this ng-repeat
is already inside another ng-repeat and $index
, $last
, $first
is probably caught by the outer ng-repeat
values instead of the inner one, as I want.
Moreover, I don't like making a HTML one-liner that will do 5 things at a time, because [1] it costs to read it and [2] it's easier to mess something, while changing, because of the complexity. So I would definitely prefer to split this into more lines. But at the same time, I don't want a space before the comma, i.e. el1 , el2
. It should be el1, el2
. And the last thing - the comma shall not be in the link, because only the element name should be linked; comma is justa separator, right?
It seems a really trivial case todo in js-templating engines (mustache, hbs, etc.), python, php, ruby, everything - and I can't find a clean solution in angular. Not to mention that builtin ng-repeat
doesn't support such fundamental feature.