我正在尝试显示字符串[],类似于
JS代码
var result = ["Fish","Mutton","Shrimp","Chicken"];
$scope.res = result;
我获取$ scope.res中的所有数组值,并使用ng-bind,
显示在HTML中HTML代码
<span ng-bind="res"></span>
结果:鱼,羊肉,虾,鸡
需要的结果:鱼,羊肉,虾,鸡
我需要在每个数组值之间添加空格。
答案 0 :(得分:6)
您可以使用join
方法。
join()方法连接数组的所有元素(或类似数组) 对象)成为一个字符串。
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(', ');
});
&#13;
<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>
&#13;