1
<li ng-repeat="address in search.result.addresses">
<a href ng-click="selectAddress(address)">
  {{address.addressLines}}
</a>
</li>

The problem is with my {{address.addressLines}}

This is currently a string array so my value on screen is printed out like

["address1","address2","address3","address4"]

But I just want it printed like

address1,address2,address3,address4

1

4 Answers 4

10

There are fundamentally 2 ways:

1) By using native angular directives:

<span ng-init="foo=[1,2,3,4]" ng-app="app">
        <span ng-repeat="f in foo">{{f}}<span ng-if="!$last">,</span></span>
</span>

2) By writing a simple filter (best way):

angular.module('app', []).filter('arrayToList', function(){
        return function(arr) {
            return arr.join(',');
        }
    });

<p>{{foo|arrayToList}}</p>

This is the working example on jsfiddle: http://jsfiddle.net/g0dmazzk/

Sign up to request clarification or add additional context in comments.

2 Comments

A word to option 2. When you use the code in your existing js, make sure to remove the square brackets! Otherwise you'd redefine your module, overriding any includes you made beforehand. Because as it is in the answer, it is the setter for the module. The getter is angular.module('app').filter(... Did cost me some hours, because the error showed up elsewhere. Probably a $injector:unpr Unknown Provider error, because of the overridden includes.
of course! that was just a quick and dirty code to explain how to create a filter for that purpose :)
3

You can do this using join also :

 <li ng-repeat="address in search.result.addresses">
        <a href ng-click="selectAddress(address)">
          {{address.addressLines.join()}}
        </a>
     </li>

Comments

2

I thing somthing like this would work...

<li ng-repeat="address in search.result.addresses">
<a href ng-click="selectAddress(address)">
  <span ng-repeat="a in address.addressLines"> {{a}},</span>
</a>
</li>    

Comments

0

To avoid dealing with the comma in the last item on the list, I used a class (label from Bootstrap) like this:

<span ng-repeat="a in address.addressLines" class="label label-default"> {{ a }} </span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.