4

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?

bknopper
  • 1,193
  • 12
  • 28
Alex Man
  • 4,746
  • 17
  • 93
  • 178
  • 1
    possible duplicate of [Remove ALL white spaces from text](http://stackoverflow.com/questions/6623231/remove-all-white-spaces-from-text) – Paolo Moretti Mar 11 '14 at 14:12
  • how come duplicate....here i'm using the replace in html angular ng-repeat......not in the script......... – Alex Man Mar 11 '14 at 14:16
  • 1
    Why you are even using an MVC framework if you insist putting the business logic in your templates? Your controller is supposed to handle it, and that's the reason why it's not working. – Paolo Moretti Mar 11 '14 at 14:44
  • because i'm using the same value with space in table title........ – Alex Man Mar 11 '14 at 14:47
  • If and only if `rows` will **always** hold values having exactly 2 spaces or less - which isn't quite reliable - then you could use something not elegant at all (and neither recommended): `{{data[rows.replace(' ','').replace(' ', '')]}}`. Where, first `replace` will remove first space, second will remove second space... But as @Paolo Moretti suggested, your controller should manage this kind of work. Why can't you keep a list of keys without spaces inside your controller and use that as key mappings? – Nicolae Olariu Mar 11 '14 at 14:58

3 Answers3

6

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.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
3

Try this:

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

Rows should already be the list item. For details of replace check here; it has actually nothing to do with angular.

Community
  • 1
  • 1
schacki
  • 9,401
  • 5
  • 29
  • 32
  • 1
    when i use `{{data[rows.replace(/ /g,'')]}}` i got `Error: a is not a function OPERATORS["/"]@http://localhost/jcp_standardwork/secure/scripts/vendor/angular/angular.js:5959 OPERATORS["/"]@http://localhost/jcp_standardwork/secure/scripts/vendor/angular/angular.js:5959 binaryFn/<@http://localhost/jcp_standardwork/secure/scripts/vendor/angular/angular.js:6292` – Alex Man Mar 11 '14 at 14:17
  • It is hard to give any futher advice at the moment. Maybe you want to provide a JSFiddle, to get further help. – schacki Mar 12 '14 at 09:06
  • I don't think Angular will interpolate regex, you would need to write a custom directive instead – Vitalij Apr 19 '16 at 19:03
0
<tr ng-repeat="data in datas"> 
  <td ng-repeat="rows in rowdatas">{{data[rows.split(' ').join('')]}}</td>
</tr>

This works for me!

A. Khaled
  • 1,468
  • 16
  • 28