Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing all spaces from angular js ng-repeat variable

In one of my angular application I will get the values when I pass the key in ng-repeat.

Here each rows in rowsdata has values like 'my file1 data', 'my file2 data', 'my file3 data'

But I need to pass it as 'myfile1data', 'myfile2data', 'myfile3data'

When I used rows.replace(' ','') it is removing only the first space like 'myfile1 data', 'myfile2 data', 'myfile3 data'

<tr ng-repeat="data in datas"> 
  <td ng-repeat="rows in rowdatas">{{data[rows.replace(' ','')]}}</td>
</tr>

EDIT

But when I use

<td ng-repeat="rows in rowdatas">{{data[rows.replace(/ /g,'')]}}</td>

I got

Error: a is not a function OPERATORS["/"]@http://loclhost/jcp_standardwork/secure/scripts/vendor/angular/a‌​ngular.js:5959 OPERATORS["/"]@http://loclhost/jcp_standardwork/secure/scripts/vendor/angular/a‌​ngular.js:5959 binaryFn/<@http://loclhost/jcp_standardwork/secure/scripts/vendor/angular/angul‌​ar.js:6292 

Can anyone show me some solution for this?

like image 737
Alex Man Avatar asked Jan 11 '23 01:01

Alex Man


1 Answers

The cleanest way to do so, that I've found, is to create a filter:

angular.module('moduleFilters', []).filter('classy', function() {
  return function(text) {
    return String(text).replace(/\s*/mg, "-");
  };
});

I've called mine classy, as in my use case, it's to make variables valid for classes. Let's call yours compress as we're compressing out spaces.

Then in your html just call

<tr ng-repeat="data in datas"> 
    <td ng-repeat="rows in rowdatas">{{rows|compress}}</td>
</tr>

Now you can call the compress filter from anywhere you import that filter.

like image 169
AncientSwordRage Avatar answered Jan 19 '23 01:01

AncientSwordRage