bafix2203
bafix2203

Reputation: 541

Split in Angular JS

Here is my code below:

 vm.getid = function(){
        $http({
            method: 'GET',
            url: 'api.json',
        })
            .then(function successCallback(data) {
                $scope.id = data.data;
                console.log($scope.id);
                $scope.split = $scope.id.split('/');
            }, function errorCallback(response) {
                console.log(response);
                console.log('error');
            });

    };

And here is my html, but it does not work :

<div ng-repeat="s in split">
  {{s}}
</div>

Plunker : http://plnkr.co/edit/g1t4pludTTIAJYKTToCK?p=preview

I want to use ng-repeat $scope.split Thanks!

Upvotes: 0

Views: 65

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77930

$scope.id is a list.

What you want to achieve is to get list of lists

The easy way to render it, to use 2 ng-repeats

What about:

<div ng-repeat="i in id">
    <div ng-repeat="s in i.split('/')">
    {{s}}
  </div>
</div>

Demo 1


Or create split list as:

$scope.split = [];
angular.forEach($scope.id, function (item) {
    $scope.split.push(item.split('/'));
});

so HTML will look like:

<div ng-repeat="sp in split">
    <div ng-repeat="s in sp">
    sub: {{s}}
  </div>
</div>

Demo 2

Upvotes: 2

Janar
Janar

Reputation: 2701

printing out id you can see it is an array, not a string

["/big_big_package","/door/cooler","/door/chair","/door","/lets/go/deeper/than/this","/lets/go/deeper","/low"]

and trying to split it gives the following error in console

$scope.id.split is not a function

you can't split an array, you probably want to split each element in the array

Upvotes: 0

Related Questions