aldimeola1122
aldimeola1122

Reputation: 818

Angular ng-repeat for comma separated strings

I've a question in angularjs.

In my controller, I've a comma separated string, and I will show it in my index.html(with ng-repeat)

Here is my controller:

myApp.controller('TabsDemoCtrl',function($scope,$http){
    $http.get('/performbatch').success(function(data) {
        $scope.string = 'Gzrgh,1,2,1,4,1,1,1,20150304,20180304';
        $scope.arrString = new Array();
        $scope.arrString = $scope.string.split(',');
    });

Here is my html :

<div ng-controller="TabsDemoCtrl">
    <td ng-repeat="icerik in arrString">
       {{icerik}}
    </td>
</div>

But I couldn't achieve that. Can you help me? Thanks in advance.

Upvotes: 6

Views: 14444

Answers (4)

Samir
Samir

Reputation: 46

<td ng-repeat="L in Labour.CWCT_Description.split(',') track by $index" >
                                        {{L}}
                                    </td>

Upvotes: 1

mohamedrias
mohamedrias

Reputation: 18566

You can't have td outside table.

It must be like

<div ng-repeat="icerik in arrString track by $index">
       {{icerik}}
</div>

Also since some are duplicates items, you must add track by $index as well

DEMO

Upvotes: 3

squiroid
squiroid

Reputation: 14017

Here is plunker for you

You need to use track by $index because it is having duplicate value in array.

ng-repeat="icerik in arrString track by $index"

Upvotes: 3

Naeem Shaikh
Naeem Shaikh

Reputation: 15715

if you want to repeat s tring saperated with ,, you shall split the string to an array and the repeat

try this:

  $scope.string = $scope.string.split(",");

this creates an array from string that is split from ,.

so your code becomes:

myApp.controller('TabsDemoCtrl',function($scope,$http){
    $http.get('/performbatch').success(function(data) {
        $scope.string = 'Gzrgh,1,2,1,4,1,1,1,20150304,20180304';
  $scope.str= str.split(",");
        $scope.arrString = new Array();
        $scope.arrString = $scope.string.split(',');
    });

Upvotes: 1

Related Questions