Adding commas in a 'concatenated' string using ng:repeat and fiters

6,249 views
Skip to first unread message

Luther Goh Lu Feng

unread,
Jul 2, 2011, 8:52:41 AM7/2/11
to angular
Based on the below html snippet, I am trying to achieve strings that
are punctuated by commas, such as 'Tom, Mary, Sue'. But unfortunately,
the code does not work and I hope to get some ideas to imrpove it.

<span ng:repeat="l in likes.$filter(filter_workspace_answers)">
{! l.user_name !}{<span ng:show="likes.length-$index==0">,</span>
</span>

Luther Goh Lu Feng

unread,
Jul 2, 2011, 8:59:12 AM7/2/11
to angular
Oops just discovered the solution. But I am happy to hear alternative
proposals :)

<span ng:repeat="l in likes.$filter(filter_workspace_answers)">
{! l.user_name !}<span ng:hide="(likes.
$filter(filter_workspace_answers).length-$index-1)==0">,</span>
</span>

Luther Goh Lu Feng

unread,
Jul 2, 2011, 9:07:22 AM7/2/11
to angular
Using $count() works too:

<span ng:repeat="l in likes.$filter(filter_workspace_answers)">
{! l.user_name !} <span ng:hide="(likes.
$count(filter_workspace_answers)-$index-1)==0">,</span>
</span>

Witold Szczerba

unread,
Jul 2, 2011, 11:14:25 AM7/2/11
to ang...@googlegroups.com
Hi,
http://docs.angularjs.org/#!/api/angular.widget.@ng:repeat
There is also $position available, so it can be written like this:

{! l.user_name !} <span ng:hide="$position == 'last' ">,</span>

or using just an $index like this:

<span ng:hide="$index == 0">,</span> {! l.user_name !}

Looks much simpler now. You could also store the element count in
controller somewhere, so you would not have to recalculate it all over
again, or even better: create a method in controller which would
result true or false, so no calculation decisions would have to be
done in view.

Regards,
Witold Szczerba

> --
> You received this message because you are subscribed to the Google Groups "angular" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Igor Minar

unread,
Jul 2, 2011, 11:29:22 AM7/2/11
to ang...@googlegroups.com
why don't you just use {{ array.join(', ') }}?


/i

Luther Goh Lu Feng

unread,
Jul 2, 2011, 2:55:55 PM7/2/11
to angular


On Jul 2, 11:29 pm, Igor Minar <iimi...@gmail.com> wrote:
> why don't you just use {{ array.join(', ') }}?
>
> http://jsfiddle.net/IgorMinar/cksDH/1/
>
> /i

Thanks for pointing it out.

likes is a json object which I need to run through the predicate
function filter_workspace_answers() for filtering. Is it still
possible to apply join() then?

Luther
>
> On Sat, Jul 2, 2011 at 8:14 AM, Witold Szczerba <pljosh.m...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Hi,
> >http://docs.angularjs.org/#!/api/angular.widget.@ng:repeat
> > There is also $position available, so it can be written like this:
>
> > {! l.user_name !} <span ng:hide="$position == 'last' ">,</span>
>

Seems that for an array of length 2, the 2nd position is 'middle', not
'last'. This will cause a bug.

> > or using just an $index like this:
>
> > <span ng:hide="$index == 0">,</span> {! l.user_name !}
>
> > Looks much simpler now. You could also store the element count in
> > controller somewhere, so you would not have to recalculate it all over
> > again, or even better: create a method in controller which would
> > result true or false, so no calculation decisions would have to be
> > done in view.
>
> > Regards,
> > Witold Szczerba
>

Yes this works great. Thanks for all the feedback!

Witold Szczerba

unread,
Jul 2, 2011, 3:18:00 PM7/2/11
to ang...@googlegroups.com
On 2 July 2011 20:55, Luther Goh Lu Feng <elf...@yahoo.com> wrote:
>> > {! l.user_name !} <span ng:hide="$position == 'last' ">,</span>
>>
>
> Seems that for an array of length 2, the 2nd position is 'middle', not
> 'last'. This will cause a bug.

In that case you should submit a bug in a first place.

Igor Minar

unread,
Jul 2, 2011, 6:38:51 PM7/2/11
to ang...@googlegroups.com
On Sat, Jul 2, 2011 at 11:55 AM, Luther Goh Lu Feng <elf...@yahoo.com> wrote:


On Jul 2, 11:29 pm, Igor Minar <iimi...@gmail.com> wrote:
> why don't you just use {{ array.join(', ') }}?
>
> http://jsfiddle.net/IgorMinar/cksDH/1/
>
> /i

Thanks for pointing it out.

likes is a json object which I need to run through the predicate
function filter_workspace_answers() for filtering. Is it still
possible to apply join() then?

I don't see why not. $filter is just angular.Array.filter method which returns an array. Each js array has the join function, so it should work.
 

Luther
>
> On Sat, Jul 2, 2011 at 8:14 AM, Witold Szczerba <pljosh.m...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Hi,
> >http://docs.angularjs.org/#!/api/angular.widget.@ng:repeat
> > There is also $position available, so it can be written like this:
>
> > {! l.user_name !} <span ng:hide="$position == 'last' ">,</span>
>

Seems that for an array of length 2, the 2nd position is 'middle', not
'last'. This will cause a bug.


/i

Luther Goh Lu Feng

unread,
Jul 3, 2011, 12:22:03 AM7/3/11
to angular
Perhaps I am unfamiliar with the syntax.

For this code snippet how do I modify it to produce 'john, jim'?

http://jsfiddle.net/IgorMinar/cksDH/1/

Igor Minar

unread,
Jul 3, 2011, 2:41:14 AM7/3/11
to ang...@googlegroups.com

Luther Goh Lu Feng

unread,
Jul 3, 2011, 3:27:58 AM7/3/11
to angular
I am so sorry Igor

My previous reply had the wrong code snippet. Could you please
reference this code snippet?

http://jsfiddle.net/cksDH/3/

Igor Minar

unread,
Jul 3, 2011, 4:27:51 AM7/3/11
to ang...@googlegroups.com
I see.. with that kind of data, I can't think of easily achieving the same without resorting to hacks.

Why don't you just create controller method that will filter and concatenate the data and return a string? It will be much cleaner that cramming all of this logic into the template.

/i

Witold Szczerba

unread,
Jul 3, 2011, 6:12:43 AM7/3/11
to ang...@googlegroups.com
A span with a comma comeback :)
http://jsfiddle.net/witoldsz/cksDH/4/

Igor Minar

unread,
Jul 8, 2011, 12:13:57 PM7/8/11
to ang...@googlegroups.com
I can't reproduce this issue: http://jsfiddle.net/IgorMinar/8fSsp/

Can you provide more info?

/i 
Reply all
Reply to author
Forward
0 new messages