Skip to content Skip to sidebar Skip to footer

Str.replace Not Working Inside 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, ' ')); But when I put the same

Solution 1:

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 :

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

Solution 2:

Using angular filter is the best practice to filter from html.

Have a look at angular-filter here

.filter('customFilter', ['$filter', function ($filter) {
    return function (input) {
      if (input) {
      	return input.replace(/_/g, '');
      }
		}
}])
<th class="text-center" ng-repeat="(key, value) in filteredEL[0] ">
    {{ key | customFilter}}
 </th>

Post a Comment for "Str.replace Not Working Inside Html"