2

I am trying to display a string[], which is like

JS Code

var result = ["Fish","Mutton","Shrimp","Chicken"];
$scope.res = result;

I get all the array values in $scope.res and displayed in HTML using ng-bind,

HTML code

<span ng-bind="res"></span>

Result: Fish,Mutton,Shrimp,Chicken

Needed Result: Fish, Mutton, Shrimp, Chicken

I need to add space between every array value.

1 Answer 1

8

You can use join method.

The join() method joins all elements of an array (or an array-like object) into a string.

JS

var result = ["Fish","Mutton","Shrimp","Chicken"];
$scope.res = result.join(', ');

HTML

<span ng-bind="res"></span>

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

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

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.