Mastering the Art of HTML Binding in Angular 8

I am facing a challenge in Angular 8 with displaying HTML content. The app I'm building in Angular 8 has a Flask backend that sends JSON data containing an HTML template to the frontend. My goal is to render this template in Angular.

Here is the JSON data being received:

`

<html>
 <head>
   <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
   <style>body{ margin:0 100; background:whitesmoke; }</style>
 </head>
 <body>
   <h1></h1>
<div>
<div id="bff3697b-6ab4-4b03-9932-87484de30381" class="plotly-graph-div" style="height:100%; width:100%;"></div>
<script type="text/javascript">

     window.PLOTLYENV=window.PLOTLYENV || {};

 if (document.getElementById("bff3697b-6ab4-4b03-9932-87484de30381")) {
     Plotly.newPlot(
         'bff3697b-6ab4-4b03-9932-87484de30381',
         [{"histnorm": "probability", "opacity": 0.8, "type": "histogram", "x": [7665.7, 16227.56, 5445.08, 4294.01, 3047.38, 5792.88, 7783.29, 10969.92, 3976.19, 13985.95, 6483.98, 5076.62, 3846.69, 7423.5, 6705.06, 4447.09, 5558.81, 5744.26, 5157.67, 5464.3, 4803.11, 6736.29, 4400.25, 4484.76, 5003.46, 6313.67, 4096.05, 7641.69, 7888.45, 13288.3, 16026.95, 5317.73, 6627.48, 10342.09, 9298.19, 5579.38, 5891.89, 5705.15, 5392.63, 5067.95, 5657.95,...

` The issue arises when trying to display the plot within the above HTML template in Angular. Saving the same template as a .html file and opening it in a browser successfully displays the plot. However, the plot does not render in Angular 8, even though the HTML template is present.

My app.component.html:

<div [innerHtml] = "someData"></div>

My app.component.ts:

import { Component, OnInit, ViewChild} from '@angular/core';

@Component({
  selector: 'app-data-decomposition',
  templateUrl: './data-decomposition.component.html',
  styleUrls: ['./data-decomposition.component.css']

})
export class DataDecompositionComponent implements OnInit {
 someData = `<html>
 <head>
   <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
   <style>body{ margin:0 100; background:whitesmoke; }</style>
 </head>
 <body>
   <h1></h1>
   <div>


<div id="bff3697b-6ab4-4b03-9932-87484de30381" class="plotly-graph-div" style="height:100%; width:100%;"></div>
<script type="text/javascript">

     window.PLOTLYENV=window.PLOTLYENV || {};

 if (document.getElementById("bff3697b-6ab4-4b03-9932-87484de30381")) {
     Plotly.newPlot(
...

Upon adding tags like <h1> or <p>, the content appears correctly in Angular 8. However, the Plotly plot within the HTML template doesn't display. Do I need to install additional packages for this functionality, considering I've already installed plotly.js for my Angular project?

Answer №1

If you're working with Angular, one way to incorporate HTML into your template is by using the innerHTML property:

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

In your Typescript component, you can set the value of theHtmlString:

@Component({
...
})
export class MyComponent {
  theHtmlString: string;

  constructor() {}

  async ngOnInit() {
    this.theHtmlString = await getHtmlPlotFromAPI();
  }  
}

It's generally recommended to retrieve raw JSON data from your server and bind it to your Angular template on the client side.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the correct way to pass an ID parameter in a GET request via HTTP?

When I began typing, I encountered a challenge: the http.get function only accepts URLs, so I am unsure how to send an ID of a person to retrieve that specific person from my API, which in turn fetches the data from a SQL database. public getByID(n :numbe ...

Tips for setting the color of hyperlinks in a UIWebView

I'm struggling with formatting a short plain text string, which may include a weblink, in a UIWebView. When I use loadHTMLString, the font style and size appear incorrect. After searching extensively, I ended up embedding the text in a makeshift HTML ...

Error message "Observable_1.Observable.throw is not a function" encountered in Angular Universal RxJs

I encountered an issue on the universal server console log specifically when using SSR (Server-Side Rendering) and not with ng serve. ERROR TypeError: Observable_1.Observable.throw is not a function Below is how my service is configured: import { Inje ...

The HTML marquee element allows text to scroll sideways or

Has the html marquee tag been phased out? If so, what are the current alternatives available for achieving a similar effect on modern browsers? I am looking to implement a basic marquee effect on my Joomla page. ...

Angular Material 2's md-table component

Currently, I am working on utilizing the Angular Material Table. Within the HTML code snippet provided, we can see: <ng-container cdkColumnDef="userId"> <md-header-cell *cdkHeaderCellDef> ID </md-header-cell> <md-cell *cdkCellDef= ...

What is causing these divs to shift apart instead of staying next to each other when the browser window is resized?

I am facing an issue with two of my divs not staying next to each other when resizing the browser window. They align perfectly when the window is wide, but as soon as I resize it to a narrower resolution, the right div moves below the left one: http://jsfi ...

jQuery animation that smoothly fades out from the left and fades in from the right

This survey is designed to assist individuals in determining where they should go with a specific type of ticket. Currently, everything is functioning smoothly, but I would like to add a sleek animation to each ul transition. Whenever a button is clicked ...

A guide on rotating loaders and inserting custom text within loaders using CSS

Seeking assistance to modify my code for creating a unique loader design. The inner loader needs to remain static with only top and bottom segments, while the middle loader rotates anti-clockwise and the outer loader rotates clockwise. Additionally, the ...

Unable to choose anything within a draggable modal window

Can someone please assist me? I am struggling to figure this out. I can't select text inside the modal or click on the input to type text into it. I suspect it may be due to z-index issues, but I'm having trouble locating them. var currentZ = n ...

The alignment of Bootstrap Select dropup with search results is off

I've encountered an alignment issue with bootstrap select live search (selectpicker). The dropup menu is misaligned when showing search results, but strangely, the alignment corrects itself after scrolling the page. Interestingly, this issue doesn&apo ...

revealing the hidden message on the back of the card

I have been working on creating flipping cards for my website using CSS, but I am facing an issue. Even after styling my cards as per the provided CSS code, I am unable to make them flip when I hover my mouse over them. .row { padding-top: 25px; } .c ...

What is the reason for this interceptor being activated only for the Sent-Event and not for the actual response?

I am dealing with multipart/mixed content from a server that contains JSON and bytes. I need to parse this content into an object in Angular. This is how I make my request: public sendRequest(headerData: string): Observable<MultipartResponse> { ...

Can a rotation animation be incorporated into an image in next.js when it is clicked?

Looking to enhance a next.js image with an animation? How about making it rotate 360 degrees upon each click? Despite my attempts at toggling classes, I can't seem to achieve the desired outcome. Any guidance would be greatly appreciated. Thank you in ...

Tips for successfully implementing CSS animations in EmberHere are some helpful ways to

I am currently using the INSPINIA administrative theme and have a question that extends from a previous one regarding installing bootstrap 4 template on an Ember web project. My query is related to getting CSS animations to work properly in Ember. I am t ...

Quick method to alter theme and store it - Ionic

How can I modify the appearance of an element by changing its color and font size when a user clicks a button? <h1> Hello Stackoverflow </h1> <button (click)="changeTheme()">Change Theme</button> What is the best approach to chang ...

The process of building an Angular package results in the creation of if(false) {...}

I'm currently working on an Angular package repository hosted on GitHub at https://github.com/marcobuschini/parking-widget. All my tests pass successfully and there are no errors when I build using ng build. However, the resulting code contains some i ...

What is the best way to animate CSS elements using jQuery to slide in from the right, left, or bottom?

When the page is scrolled on my website, I want table_area-left to move from left to right to its current position, table_area-right to move from right to left to its current position, and morph button to move from down to up. I am struggling to find the j ...

Adding a background behind the currency symbol before the textbox field

I need to customize a text field like the one shown in the image below: https://i.sstatic.net/gu3JA.png After making some quick adjustments, I ended up with the following result. My main concern is now the background of the currency symbol. https://i.ss ...

Script execution is disabled on this system preventing the loading of content - ANGULAR V14

Every time I try to run my Angular project or any ng command, I keep encountering the following error message: ng : File C:\Users\achra\AppData\Roaming\npm\ng.ps1 cannot be loaded because running scripts is disabled on this ...

Setting the height of a div using HTML and CSS

Can someone please assist me with my html/css code? I am having trouble making the height of my divbody match the height of divwrapper. Currently, the height of divbody is only 10 (with padding valued at 5px). Here is the code snippet, thank you! <h ...