0

How can I render an expression inside an [innerHTML] directive:

public stringInterpolation: string = 'title';
public data: any = '<a>{{stringInterpolation}}</a>';
<div [innerHTML]="data"></div>

String interpolation is being rendered as text not the exact value.

Logic behind this is I am using a reusable table and I want a configuration where in I can specify a template that will be generated inside a cell rather than listing all data as text.

2
  • 4
    Possible duplicate of Angular2 - Interpolate string with html Commented Mar 14, 2017 at 8:04
  • Sorry. but i am looking for a way to read the value of a string interpolation inside the innerHtml Commented Mar 14, 2017 at 8:05

2 Answers 2

1

You can use TypeScript interpolation. Angular bindings aren't supported in dynamically added HTML:

public data: any = `<a>${stringInterpolation}</a>`;
Sign up to request clarification or add additional context in comments.

2 Comments

You should be aware that this only binds a single time when data is initialized in contrary to {{}} which updates every time stringInterpolation changes.
sir can you tell mw how to iterate array with TypeScript interpolation?.actually i have an array like this---*ngFor="let item of ${salesOrderModel.SalesOrderItemList}"?
0

You can't do this, if you have to inject different templates inside an html tag. You have to create one component for each template you need, and load components dynamically.

To load component at runtime, see this: How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

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.