0

I want to load the html from my asset folder in between my html file of angular. For eg: Suppose this is my product.html of product component and detailPara.html and BlockPara.html are in assets folder which I have to add in between the product.html:

<div class="productPage">
....
<div innerHTML="../assets/product/detailPara.html"></div>
....
<div innerHTML="../assets/product/BlockPara.html"></div>
</div>

Can someone please help me.

4
  • cant you create a component calls DetailParaComponent, BlockParaComponent and call them inside the parent ? Commented Nov 10, 2021 at 12:26
  • Can you please explain more I am not understanding,I am new to angular also, I already have product component in whose product.html I want to add the detailPara.html and blockPara.html from assets folder, these are static data. Commented Nov 10, 2021 at 12:36
  • @RinkiMishra check my post below If you any doubt then comment me I will help you. Commented Nov 10, 2021 at 12:41
  • If you want the inner html. you have to use the DOMSanitizer or else you can't use it. You have to bypassSecurityTrustHtml. Commented Nov 10, 2021 at 13:28

2 Answers 2

1

you can use it Like this.

let path = 'assets/' + {fileName} + '.html';
this._httpClient.get(path, {responseType: "text"}).subscribe(
data => {
//now you have the file content in 'data'
this.content = this.sanitizer.bypassSecurityTrustHtml(data);
});

just inject the DOMSanitizer in the constructor and you are good to go!!

and then in your HTML file.

<div class="productPage">
....
<div [innerHTML]="content"></div>
....
Sign up to request clarification or add additional context in comments.

3 Comments

But I cannot make the h2 colour of the html inside the assets folder default as green by writting it in product.scss?
I have another question if the .html is not present in asset how can i show another div without causing my code to error
you can just use ngIF* and remove the innerhtml div from HTML file
1

Firstly, create a component with name detailPara check below command to create detailPara:

ng generate component detailPara

Or:

ng g c detailPara

Both are same after that paste your detailPara.html code to that detailPara component and same for BlockPara.html create component with below command:

ng generate component BlockPara

Or:

ng g c BlockPara

and paste BlockPara.html code to BlockPara component.

and finally use like this:

<div class="productPage">
  <app-detail-para></app-detail-para>  // check before pasting

  <app-block-para></app-block-para>    // check before pasting
</div>

Second Way

Using JavaScript

 document.getElementById('detailPara').innerHTML =
      '<object type="text/html" data="assets/detailPara.html" ></object>';

    document.getElementById('blockPara').innerHTML =
      '<object type="text/html" data="assets/BlockPara.html" ></object>';
<div class="productPage">
  <div id="detailPara"></div>

  <div id="blockPara"></div>
</div>

Here you can check Stackblitz.

4 Comments

I actually want both the detailPara.html and BlockPara.html to be in assets folder only, because I want the option to change it, without creating the component can I directly access it from asset folder and add it in my main html file
ok then give me sometime I will edit my post
Thankyou for the solution, I just have one more question can we make the h1 color as green as default in app.css rather than doing it in detailPara.html
No you can't do that

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.