str.replace在html中不起作用 [英] str.replace not working inside html

查看:107
本文介绍了str.replace在html中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的角应用中的字符串替换所有 _ 。在我的控制器中,以下代码给出了正确的结果:

I need to replace all _ from a string in my angular app. In my controller, following code gives correct result:

alert(this_is_string _。replace(/ _ / g,));

但是当我在我的html文件中放入相同的代码时,这样:

But when I put the same code in my html file like this:

 <th class="text-center" ng-repeat="(key, value) in filteredEL[0] ">
    {{ key.replace(/_/g, ' ') }}
 </th>

它出现以下错误:

Error: [$parse:syntax] Syntax Error: Token '/' not a primary expression at column 13 of the expression [key.replace(/_/g, ' ')] starting at [/_/g, ' ')]

那么,我怎样才能使用替换所有必需的替换功能html中的实例?

So, how can I use replace function that replaces all required instances inside the html?

推荐答案

只需创建专用过滤器:

angular.module('filters.stringUtils', [])

.filter('removeUnderscores', [function() {
    return function(string) {
        if (!angular.isString(string)) {
            return string;
        }
        return string.replace(/_/g, '');
    };
}])

并称之为:

<div id="{{'hi_there'| removeUnderscores}}"></div>

这篇关于str.replace在html中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆