Angular print string array without quotes
Asked Answered
Q

3

10

I have a simple string array coming from a server:

[{"things":["me", "my"]}]

In my page, to display the array I have:

{{things}}

And it prints:

["me", "my"]

How do I control the printing, for instance, if I want to eliminate the brackets and quotes?

Quag answered 2/12, 2014 at 0:14 Comment(0)
T
18

You can implement a scope function to display arrays as comma separated strings like shown in this fiddle.

$scope.array = ["tes","1","2","bla"];

$scope.arrayToString = function(string){
    return string.join(", ");
};

Now you can call this function within the template:

{{arrayToString(array)}}

Update

You can also use the join() method of arrays directly within the template without using an extra function inbetween as displayed within the updated fiddle.

{{array.join(", ")}}
Tavel answered 2/12, 2014 at 0:29 Comment(0)
W
9

I think you'll want ngRepeat for something like:

<div class="list" ng-repeat="thing in things">
  {{ thing }}
</div>
Wistrup answered 2/12, 2014 at 0:19 Comment(0)
K
4

You can also create custom angular filter optionally with some advanced formatting:

module.filter('formatArray', ['OptionalInjection', function(OptionalInjection) {
  return function(value) {
    if (!angular.isArray(value)) return '';
    return value.map(OptionalInjection.formatter).join(', ');  // or just return value.join(', ');
  };
}])

Then in html just write {{yourArrayValue | formatArray}}.

Kizzee answered 20/1, 2016 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.