Doapper Doapper - 3 months ago 25
AngularJS Question

ng-repeat input with comma separator

I have a string in this format

string = "1,3,12,33"


I want to simply do a ng-repeat over the values 1 2 12 33 and my code is

<div ng-repeat="number in string" ng-if="number!=','">
<a class="item item-icon-right font-12">
<b>{{number}}</b>
</a>
</div>


The problem that my code shows

1
2
1 (should show 12)
2
3
3 (should show 33)


Any help^would be appreciated

Answer

You can do this by spliting the string with the ','

JS:

app.controller('MainCtrl', function($scope) {
   $scope.string = '1,3,12,33';
        $scope.arrString = new Array();
        $scope.arrString = $scope.string.split(',');
});

View:

  <body ng-controller="MainCtrl">
    <p ng-repeat="val in arrString track by $index">
       {{val}}
    </p>
  </body>

Working Plunker