简体   繁体   中英

Add space between string array using angularjs

I am working in angularjs project,

var res = ["Art","Logic","Science"];
$scope.output = res;

Result is displayed as

Art,Logic,Science

I need the result as -

Art, Logic, Science

Have to add space between each string array value.

You can use the join() method to achieve this.

 var myapp = angular.module('myapp', []); myapp.controller('SpaceCntrl', function($scope) { var result = ["Art", "Logic", "Science"]; $scope.res = result.join(', '); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myapp"> <div ng-controller="SpaceCntrl"> <span ng-bind="res"></span> </div> </div> 

This is how I usually do it:

$scope.output = res.join(', ');

Using Array.propotype.join()

Joining the array will do it :)

var res = ["Art","Logic","Science"];
$scope.output = res.join(', ');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM