Search code examples
javascriptangularjsreplaceng-bind-html

ng-bind-html breaks when using Javascript replace() with global value


I'm trying to use ng-bind-html in hand with JavaScript's replace() function. It works just fine when I don't include a global value in replace(), but the moment I include something like replace(/test/g, 'TEST'), I get this in the console:

Syntax Error: Token '/' not a primary expression at column 95

This is what I'm trying to do, and also what gives me the error:

ng-bind-html="(resume.address == null || resume.address == '') ? 'Mailing Address' : resume.address.replace(/;/g, 'TEST')"

Have I made an obvious error that I am overlooking, and if so, what is the proper way to write this?

Edit:

My end goal is to replace a string, which contains multiple ";" characters, and have those characters be replaced with break elements that AngularJS will not sanitize into a string literal. If there is a better way of doing this, that answer is also welcome.


Solution

  • This doesn't answer your question as to why angular throws the error. But it might help you get around it.

    Create a filter:

    angular.module('yourApp', []).filter('addressFilter', function() {
      return function(input) {
        input = input || 'Mailing Address';
        return input.replace(/;/g, 'TEST')";
      };
    })
    

    HTML:

    ng-bind-html="resume.address | addressFilter"