1

I am using HTML templating in my Angular app and my code is something like this:

<script type='text/html' id="sampleId">...</script>

But, when the HTML was loaded, I couldn't find this script block in the DOM.

How can I achieve HTML templating mechanism in Angular?

Arun Raj R
  • 2,197
  • 3
  • 13
  • 23

1 Answers1

1

Angular don't allow script tag in HTML template. ANgular compiles HTML templates too and removes script tag and other security related things from template. I think you want to inject dynamic HTML in your component. Please describe your use case so that we can provide a better solution of your problem.

Sandip Jaiswal
  • 3,428
  • 2
  • 13
  • 15
  • Actually, I need to implement a templating mechanism in my page. Meaning, I need to load the HTML content inside that script block in the page with out considering it as a normal HTML (This is for a reusing purpose only). If i change that script block to a div, it will work but the browser will consider it as a normal HTML and display it in the browser. – Arun Raj R Dec 20 '17 at 08:16
  • If I change it to a div and i can also add some hidden class to the div, this will also work but, I will break my some other functionality, so , thats why i choose HTML templating. But i dont no how it possible in angular4. – Arun Raj R Dec 20 '17 at 08:19
  • In Angular we use ngIf to show and hide HTML from DOM. the good thing about ngIf is that it totally remove portion of HTML from dom based on condition. There is also a way to use which will render your html inside ng-content. We use ng-content in senario when we have component and we want some html from parent. – Sandip Jaiswal Dec 20 '17 at 09:13
  • Can you just tell me how this will help for my case. – Arun Raj R Dec 20 '17 at 11:13
  • Suppose you created a component named DemoCompoent with selector "app-demo". In your html of component put You will use this component in other component like this:
    //some html code
    HTML code inside of will render in
    – Sandip Jaiswal Dec 20 '17 at 11:17
  • I think my scenario is something different. Can you once again refer my first comment please. – Arun Raj R Dec 20 '17 at 12:00