0

My js object contains double quotes so I store data in the form:

var data = "un <a ui-sref="app.concept({id:1})">test link<\/a>"

which means <a ui-sref="app.concept({id:1})">test link<\/a>

how can I decode the html entities and then compile the ui-sref? Using this https://github.com/incuna/angular-bind-html-compile only doesn't work.

It prints <a ui-sref="app.concept({id:1})">test link<\/a> in the view

2
  • why do you store data like this? Commented May 2, 2017 at 16:55
  • My data contains this anchor. If I try to escape the double quotes of the ui-sref the result of the directive compiling is: <a class>test link</a>. Maybe I miss something.. Commented May 2, 2017 at 18:29

1 Answer 1

0

I think it may be better/easier to store that data as an actual unescaped html string. This would require much less work.

However, if you can't do that, then you may want to inject the $sce service into your controller and then use the data = $sce.trustAsHtml(data) after your data variable declaration.

After doing this, use <div ng-bind-html="data"></div> in the view. It should fix your issue.

If that doesn't completely work, you can try to unescape the data variable using a function in underscore called unescape. So trustAsHtml(_.unescape(data)) can help you.

Router UI addition

Since you are using router-ui, your best bet is probably to do as suggested in this answer and just use href="path/to/state1" if possible.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a ui-sref directive in the string, ng-bind-html doesn't compile.
@alfredopacino I just modified my answer. I think that if you are going this route, then you may just want to do what I suggest and not use ui-sref at all.
yeah I guess I would use the href solution (we know it's not the best option but it's better than encode the quotes..)

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.