naik3
naik3

Reputation: 309

Parsing a Json array in angualrjs

I have a json data of the formate

 $scope.jsondata = [{filename:"/home/username/textfiles/0101907.txt"},{filename:"/home/username/textfiles/0124757.txt"},{filename:"/home/username/textfiles/0747332.txt"} ... ];

And my HTML code is :

<li ng-repeat="x in jsondata">
   {{x.filename}}                     
</li>

When i run the above code I am getting full path. like "/home/username/textfiles/0101907.txt". I just want file name like "0101907.txt", instead of full path. Can any one help me in parsing the JSON array. Split the path and get only the file name. Thanks.

Upvotes: 0

Views: 108

Answers (4)

Jins Peter
Jins Peter

Reputation: 2469

This will solve

<li ng-repeat="x in jsondata">
{{x.filename.split("/").reverse()[0]}}
</li>

Upvotes: 0

tanmay
tanmay

Reputation: 7911

I would do it using .lastIndexOf. Like this:

<li ng-repeat="x in jsondata">
   {{x.filename.substr(x.filename.lastIndexOf('/')+1}}                     
</li>

Upvotes: 0

bhuvan salanke
bhuvan salanke

Reputation: 163

You need to split the string using "/" and then pop the file name from the array.

<li ng-repeat="x in jsondata">
   {{x.filename.split("/").pop()}}                     
</li>

Upvotes: 5

Agam Banga
Agam Banga

Reputation: 2693

You can update it via regex. Create a controller method like getFileName & use it in html like below

function getFileName(path) {
   return path.replace(/^.*[\\\/]/, '');
}

HTML

Use it like

<li ng-repeat="x in jsondata">
   {{getFileName(x.filename)}}                     
</li>

Upvotes: 1

Related Questions