str.replace not working inside html

前端 未结 2 1522
眼角桃花
眼角桃花 2021-01-20 12:49

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(

2条回答
  •  佛祖请我去吃肉
    2021-01-20 13:04

    Just create a dedicated filter :

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

    and call it like :

提交回复
热议问题