1

My problem is that in angular 4 there are few ways to data bind with HTML to TS like {{myText}}, [], () and other than those we can use [innerHTML]="myText"

what is the best way to bind simple variable to HTML among {{}}, [innerHTML] ?

2
  • they are used for different purposes. expression binding ({{}}) outputs raw text, while [innerHTML] outputs HTML formatted text. What is "best" depends on what the variable contains. Commented Oct 3, 2017 at 3:35
  • if it is a just a text ? Commented Oct 3, 2017 at 3:52

2 Answers 2

3

Try like this :

<div [innerHTML]="htmlString"></div>

in typescript file :

htmlString: string = "Hello world"; <!-- if want to display string -->
htmlString: string = "<h1>Hello world</h1>"; <!-- if want to display html element -->
Sign up to request clarification or add additional context in comments.

Comments

0

Bind it with {{yourText}} is it is not containing any HTML markups

Bind it with [innerHTML] = "youtText" if it has any HTML markups

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.