1

I want to add html code from a variable inside a for loop in angular 2:

<ul>
   <li *ng-for="#c of myHtmlItems">
      {{c.myHtml}}
   </li> 
</ul>

The html is only added as a string

1
  • In alpha.52 ng-for becomes ngFor Commented Dec 11, 2015 at 1:48

3 Answers 3

4

Update to angular2.0.0

<ul>
    <li *ngFor="let c of myHtmlItems" [innerHTML]='c.myHtml'></li>
</ul>
  1. *ng-for has been replaced with *ngFor
  2. # has been replaced with let
  3. inner-html with innerHTML (better to use property binding like this [innerHTML]='c.myHtml' instead of inner-html="{{c.myHtml}})
Sign up to request clarification or add additional context in comments.

Comments

2

you could do:

<ul>
    <li *ng-for="#c of myHtmlItems" inner-html="{{c.myHtml}}></li>
</ul>

4 Comments

this works but removes all other html already exists in the child node
you could create element inside li, eq. div and set it's inner html
I have other tags (x3dom) and only mentioned <ul> as an example. But it works for now. Thanks.
In alpha.52 ng-for becomes ngFor
-2

instead of ng-for, give a try with ng-repeat. As mostly ng-repeat is useful in very different ways

      <ul>
         <li ng-repeat="element in myHtmlItems">
            {{element}}
         </li> 
      </ul>

3 Comments

DOM elements can be repeated with ng-repeat
ng-repeat in Angular 2? your code still has *ng-for
it is from angular version 1.

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.