Javascript 删除重复变量中的所有空格
在我的一个角度应用程序中,当我在ng repeat中传递键时,我将获得值 在这里,rowsdata中的每个行都有类似于Javascript 删除重复变量中的所有空格,javascript,angularjs,Javascript,Angularjs,在我的一个角度应用程序中,当我在ng repeat中传递键时,我将获得值 在这里,rowsdata中的每个行都有类似于的值“我的文件1数据”、“我的文件2数据”、“我的文件3数据” 但我需要将其作为'myfile1data'、'myfile2data'、'myfile3data'传递。 <tr ng-repeat="data in datas"> <td ng-repeat="rows in rowdatas">{{data[rows.replace(' ','')
的值“我的文件1数据”、“我的文件2数据”、“我的文件3数据”
但我需要将其作为'myfile1data'、'myfile2data'、'myfile3data'传递。
<tr ng-repeat="data in datas">
<td ng-repeat="rows in rowdatas">{{data[rows.replace(' ','')]}}</td>
</tr>
当我使用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>
{{data[rows.replace('',)]}
编辑
但是当我使用
<td ng-repeat="rows in rowdatas">{{data[rows.replace(/ /g,'')]}}</td>
{{data[rows.replace(//g',)]}
我得到
错误:a不是函数运算符[“/”]@http://loclhost/jcp_standardwork/secure/scripts/vendor/angular/angular.js:5959运算符[“/”]@http://loclhost/jcp_standardwork/secure/scripts/vendor/angular/angular.js:5959 binaryFn/试试这个:
rows.replace(/ /g,'')
<tr ng-repeat="data in datas">
<td ng-repeat="rows in rowdatas">{{rows.replace(/ /g,'')}}</td>
</tr>
rows.replace(//g',)
{{rows.replace(//g',)}
行应该已经是列表项。有关更换检查的详细信息;它实际上与角度无关。我发现,最干净的方法是创建一个过滤器:
angular.module('moduleFilters', []).filter('classy', function() {
return function(text) {
return String(text).replace(/\s*/mg, "-");
};
});
我把我的称为classy,就像在我的用例中一样,它是为了使变量对类有效。在压缩空格时,让我们调用您的compress
然后在html中只需调用
<tr ng-repeat="data in datas">
<td ng-repeat="rows in rowdatas">{{rows|compress}}</td>
</tr>
{{行|压缩}
现在,您可以从导入过滤器的任何位置调用compress
过滤器。
<tr ng-repeat="data in datas">
<td ng-repeat="rows in rowdatas">{{data[rows.split(' ').join('')]}}</td>
</tr>
{{data[rows.split(“”).join(“”)]}
这对我有用 可能会重复为什么会重复…这里我使用html中的替换…而不是脚本中的替换…如果坚持将业务逻辑放在模板中,为什么还要使用MVC框架?您的控制器应该处理它,这就是它不起作用的原因。因为我在表标题中使用了相同的值和空格……。如果且仅当行
始终包含正好有2个空格或更少的值-这不太可靠-那么您可以使用一点也不优雅的值(也不推荐使用):{{data[rows.replace('',).replace('',)]}
。其中,第一个replace
将删除第一个空格,第二个将删除第二个空格。。。但正如@Paolo Moretti所建议的,您的控制器应该管理此类工作。为什么您不能在控制器中保留一个没有空格的键列表并将其用作键映射?当我使用{data[rows.replace(//g',)]}
时,我得到错误:a不是函数运算符[“/”]@http://localhost/jcp_standardwork/secure/scripts/vendor/angular/angular.js:5959 运算符[“/”]@http://localhost/jcp_standardwork/secure/scripts/vendor/angular/angular.js:5959 2.目前很难给出进一步的建议。也许你想提供一个JSFIDLE,以获得进一步的帮助。我不认为Angular会插入正则表达式,你需要编写一个自定义指令