Skip to main content
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Eileen Koven
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Eileen Koven ( @psychotropek )

Adding An ngRepeat List Delimiter In AngularJS

Published in Comments (14)

Recently, I played around with an AngularJS directive that compiled and transcluded its target element. That experiment left me a little confused as to how the compile step actually worked; so, I wanted to take a step back and look at compiling an element with fewer facets. This time, rather than transcluding, I'm simply going to compile the DOM, adding an additional HTML element. Specifically, I'll be adding a list delimiter to the end of an ngRepeat template.

In my post on using the ngController directive in conjunction with the ngRepeat directive, I added a Span tag to [one of] my ngRepeat lists:

<span ng-repeat="friend in selectedFriends">

	{{ }}

	<span ng-show=" ! $last ">-</span>


This Span tag took care of conditionally showing a list delimiter based on the index of the ngRepeat iteration. You'll notice that the delimiter is a dash, rather than the more commonly-used, "comma." This is because a comma would look strange unless it was butted directly up against the content in the ngRepeat:

<span ng-repeat="friend in selectedFriends">

	{{ }}<span ng-show=" ! $last ">,</span>


To me, this code is less visually pleasing; and code that is less visually pleasing makes me die a little bit inside. As such, I thought this would make a good AngularJS directive - I could encapsulate the awkward placement of the Span inside an easy-to-read attribute.

In the following demo, I'll be adding the same Span tag; however, I'll be adding it in a compile step within the bnRepeatDelimiter directive. Notice that the the trailing whitespace of the ngRepeat template is maintained, even as the delimiter is injected.

<!doctype html>
<html ng-app="Demo" ng-controller="DemoController">
	<meta charset="utf-8" />

		Adding An ngRepeat List Delimiter In AngularJS

		Adding An ngRepeat List Delimiter In AngularJS

	<form ng-submit="addFriend()">

		<input type="text" ng-model="newFriendName" size="20" />

		<input type="submit" value="Add Friend" />


	<p ng-show="friends.length">


			Notice that we define the comma as the list delimiter
			for our ngRepeat directive.
			ng-repeat="friend in friends"

			{{ friend }}



	<!-- Load jQuery and AngularJS from the CDN. -->

	<!-- Load the app module and its classes. -->
	<script type="text/javascript">

		// Define our AngularJS application module.
		var demo = angular.module( "Demo", [] );

		// -------------------------------------------------- //
		// -------------------------------------------------- //

		// I am the main controller for the application.
			function( $scope ) {

				// -- Define Scope Methods. ----------------- //

				// I add a new friend to the list and reset the model.
				$scope.addFriend = function() {

					$scope.friends.push( $scope.newFriendName );

					$scope.newFriendName = "";


				// -- Define Scope Variables. --------------- //

				// I am the list of friends to show.
				$scope.friends = [ "Sarah" ];

				// I am the name of the new friend being added.
				$scope.newFriendName = "Joanna";


		// -------------------------------------------------- //
		// -------------------------------------------------- //

		// I add a delimiter to the end of the list items. This is
		// designed to be used in conjunction with ngRepeat. It will
		// add the delimiter to all list items; but it will only show
		// on all but the $last ngRepeat item.
			function() {

				// I compile the list, injecting in the conditionally
				// visible delimiter onto the end of the template.
				function compile( element, attributes ) {

					// Get the delimiter that goes between each item.
					var delimiter = ( attributes.bnRepeatDelimiter || "," );

					// The delimiter will show on all BUT the last
					// item in the list.
					var delimiterHtml = (
						"<span ng-show=' ! $last '>" +
							delimiter +

					// Add the delimiter to the end of the list item,
					// making sure to add the existing whitespace back
					// in after the delimiter.
					var html = element.html().replace(
						function( whitespace ) {

							return( delimiterHtml + whitespace );


					// Update the compiled HTML.
					element.html( html );


				// Return the directive configuration. Notice that
				// our priority is 1 higher than ngRepeat - this will
				// be compiled before the ngRepeat compiles.
					compile: compile,
					priority: 1001,
					restirct: "A"




As you can see, I am using the jQuery html() method to read and then replace the content of the compiled element. Furthermore, I am capturing the trailing whitespace of the ngRepeat element so that I can maintain it even after I inject my Span-based delimiter. This allows me to butt the delimiter right up against the rendered content of the repeater without corrupting the overall source.

In this experiment, I have configured AngularJS to compile my directive before the ngRepeat directive is compiled. Since ngRepeat compiles with a priority of 1000, the bnRepeatDelimiter - with a priority of 1001 - will compile first. Based on the highly dynamic nature of ngRepeat, this feels like the right order of operations. But, as it turns out, having a priority lower than 1000 leads to the same outcome. I suppose that it because the ngRepeat element is only compiled once, no matter what - its repetition is based on clone-transclusion during the linking phase, not the compile phase.

Clearly, this kind of list delimiter would only work with certain kinds of lists (namely inline, textual lists). But, the real point here was simply to explore the compile phase of the compile-link lifecycle. AngularJS directives hold a tremendous amount of power; but, really mastering them is going to take a good deal of time and practice.

Want to use code from this post? Check out the license.

Reader Comments



Thanks my man! Will do!


Oh snap, good eyes! Looks like my spell-checker didn't catch that one. It seems to be weird about what it will / won't check.


Hi Ben, I really love your posts on angularjs.
It seems that every time something new catches my eyes you either have just started looking into it or will start shortly after - Sometimes I even just wait with diving into the subject until after you have dug the hole a little deeper :)

On a side note to this post. Do you need jquery for the html manipulations when angularjs comes with an jquerylite addition?


Hi Ben,

I changed the priority of the


to 999, but i'm getting the same html strucutre, as when it was in 1001. Could you please explain how that differs.



I guess great minds think alike ;)

As far as the jQuery "lite" that comes with AngularJS, there are things you can do out of the box and things you can't do. I've been hit by the difference a number of times, so I just include the core jQuery by default (even if I don't need it). Then, if I have to add something like show() or hide() or slideDown() or a find() with CSS selectors, I don't have to then go back and add jQuery.

But, as long as you're not doing too much CSS oriented, I think the built-in "lite" version is pretty good.



When I first wrote this, I used 1001 because it *felt* right to have to have it compile *before* the ngRepeat compiles. But, as you have seen, 1001 vs. 999 makes no difference.

I'm still trying to fully-understand the compile/link life-cycle, but I think in this case it doesn't make a difference since the ngRepeat directive only compiles a single element as well. Even though ngRepeat can result in multiple elements, those only appear in the LINK phase as part of the cloning process, after the compile has taken place.

I know that probably didn't help clarify it too much; and that's because my understanding isn't too strong yet.


Hey Ben,

Your demo's on angular have been very useful. When you start needing angular to do things that normally were easy in backbone/jquery it gets pretty confusing, but you seem to be figuring it out pretty well, and its helpful referring to these posts for tips and clarification.

My question isn't really angular related but something i have been curious about for some time while reading your posts. I noticed that you usually wrap any variables or objects, any return statements in parentheses. I'm just wondering is that a preference of yours, or is it better javascript coding practice? Mostly wondering if its something i should be doing that i dont know about.


var delimiterHtml = (
	"<span ng-show=' ! $last '>" +
		delimiter +

Thank you



First, I'm glad that you are finding these posts helpful. AngularJS is pretty darn awesome; but, when you want to do some more UI-based shenanigans, it definitely makes you jump through a few more hoops than something like jQuery (vanilla) or other JS frameworks might. That said, I love that it forces you to keep a stricter separation of concerns.

The one thing that I wish I could do is "namespace" my directives. I haven't really come up with a good strategy for that yet.

Ok, so as for the formatting, ALL of that stuff is just personal preference. If you continue to read my blog, you'll come to find that I love a few things:

* White space.
* Comments (more in my demos than in my production code).
* Parenthesis to group anything that seems groupable.
* Parenthesis around my return values.
* Semicolons.
* Line lengths of less than or equal to 70 chars (for the blog content display).

But, that's all personal preference. Some of it is influenced by the fact that I'm "thinking outloiud" in my code, hence the comments. I happen to think that my choices make my code more readible... for ME, but others downright dislike the way I do things :)


Excellent post, directives are definitely the trickiest part of Angular to wrap your head around.

The comma-delimited ngRepeat is an actual use case I've had recently as well and found CSS to be a suitable enough for my particular situation.

<span class="friend" ng-repeat="friend in selectedFriends">
	{{ }}
.friend::after { content: ", " }
.friend:last-child::after { content: "" }
/* or */
.friend::before { content: " ," }
.friend:first-child::before { content: "" }


Awesome use of CSS :) I've only recently started to use :before / :after pseudo-elements and they are hugely useful. I haven't gotten too much into the last/first child stuff.

Honestly, I did not realize that you could *override* the *content* value of the :after element. That's pretty bad ass! Thanks for the tip!


i need some like this:

{% for foo in bar %}
{% if foo == 'some' %}
{% endif %}
<li>{{ foo }}</li>
{% endfor %}

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel