0

i try to inject html content from a class but instead the result i get is a string instead of the html element, what am i doing wrong?

angular version : angular-cli: 1.0.0-beta.20-4

html content:

<div [ngStyle]=getStyles()>{{getData()}}</div>

getData() return value:

"<span style='color:#5999B6'>this.data</span>"

this.data is a string

1
  • how does your getData() function looks like? what are you actually returning? Commented Dec 29, 2017 at 12:52

2 Answers 2

2

Angular don't allow direct injection of HTML in template for security reason. You need to sanitize your html. Follow following url to sanitize your HTML. https://netbasal.com/angular-2-security-the-domsanitizer-service-2202c83bd90

Hope it will help

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

1 Comment

In fact, it does, I'm using it in production ^^. The issue is that the topic you linked asks to bypass the sanitization before injecting the string to template. If this string is linked to a user input, it'll lead to XSS injection for sure. The sanitizer is supposed to be here for that.
0

You want to bind html using innerHTML:

<div [ngStyle]="getStyles()" [innerHTML]="getData()"></div>

But BE CAREFUL, you have to have full control over the html returned by getData() to avoid XSS issues.

Comments

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.