0

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.

4
  • This might be a good place to use a custom angular filter Commented Nov 3, 2015 at 0:58
  • @o4ohel You might be right, but I'm still not entirely familiar with how to write directives. I have a basic idea, but I'm not sure how I would do something like this. Commented Nov 3, 2015 at 0:59
  • filters are easier to write then directives: docs.angularjs.org/guide/filter . Basically, you will write a function that takes a string and returns another string. So the logic you currently have inline will just move to the filter. ng-bind-html="resume.address | your_filter" Commented Nov 3, 2015 at 1:04
  • @o4ohel Thanks for your feedback! I will try this. I'll leave the question open though, I am very curious as to why Angular is not working with this expression. Commented Nov 3, 2015 at 1:06

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this does work for what I need. I would +1 this, but I'm so new to this site that it does not allow me to for the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.