Garuuk
Garuuk

Reputation: 2254

truncate filter to shorten URL

I have an ng-repeat that displays a bunch of long url paths in an input text box.

<div ng-repeat = path in paths>
    <input type="text" value="{{path}}"> 
</div>

the paths looks like this

endpoint:8000/foo/foo/foo/charlie1.file

I would just like to display the charlie1.file part

I could do it with regex and I even successfully made a function that splits it the path, reverses it, then does a for loop that pushes each character to an array, catches the first "/" then breaks the function, then re-reverses it. Then I have to loop through each path in my api call with this split/reverse function which is cumbersome.

Is there an easier way filter/truncate in angular to achieve this without a bunch of for loops?

Upvotes: 0

Views: 742

Answers (1)

PSL
PSL

Reputation: 123739

You could just create a simple filter:-

 app.filter('trunc', function(){
     return function(input){
       if(!angular.isString(input)) return;
       return input.split('/').pop();
     }
 });

and do:-

 <input type="text" value="{{path | trunc}}"> 

Upvotes: 1

Related Questions