Angular print array of strings without quotes

I have a simple array of strings coming from the server:

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

On my page, to display an array, I:

 {{things}} 

And he prints:

 ["me", "my"] 

How to manage printing, for example, if I want to exclude brackets and quotation marks?

+7
arrays angularjs angularjs-templates
source share
3 answers

You can implement the scope function to display arrays as comma-separated strings, as shown in this fiddle .

 $scope.array = ["tes","1","2","bla"]; $scope.arrayToString = function(string){ return string.join(", "); }; 

Now you can call this function in the template:

 {{arrayToString(array)}} 

Update

You can also use the join() array method directly inside the template without using the additional inbetween function displayed in the updated script .

 {{array.join(", ")}} 
+17
source share

I think you need ngRepeat for something like:

 <div class="list" ng-repeat="thing in things"> {{ thing }} </div> 
+8
source share

You can also create your own angular filter with some pre-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}} {{yourArrayValue | formatArray}} .

+4
source share

All Articles