javascript 从angular js ng-repeat变量中删除所有空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22328214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing all spaces from angular js ng-repeat variable
提问by Alex Man
In one of my angular application I will get the values when I pass the key in ng-repeat.
在我的一个 angular 应用程序中,当我在 ng-repeat 中传递密钥时,我将获得这些值。
Here each rowsin rowsdatahas values like 'my file1 data', 'my file2 data', 'my file3 data'
这里,每个排在rowsdata具有类似值'my file1 data', 'my file2 data', 'my file3 data'
But I need to pass it as 'myfile1data', 'myfile2data', 'myfile3data'
但我需要通过它作为 'myfile1data', 'myfile2data', 'myfile3data'
When I used rows.replace(' ','') it is removing only the first space like 'myfile1 data', 'myfile2 data', 'myfile3 data'
当我使用 rows.replace(' ','') 时,它只删除第一个空格,例如 '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?
谁能告诉我一些解决方案?
采纳答案by schacki
回答by Pureferret
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 compressas we're compressing out spaces.
我称我的为 classy,就像在我的用例中一样,它是为了使变量对类有效。compress当我们压缩空间时,让我们打电话给你。
Then in your html just call
然后在你的 html 中调用
<tr ng-repeat="data in datas">
<td ng-repeat="rows in rowdatas">{{rows|compress}}</td>
</tr>
Now you can call the compressfilter from anywhere you import that filter.
现在,您可以compress从导入该过滤器的任何地方调用该过滤器。
回答by amrography
<tr ng-repeat="data in datas">
<td ng-repeat="rows in rowdatas">{{data[rows.split(' ').join('')]}}</td>
</tr>
This works for me!
这对我有用!

