Apply CSS styles by either passing them in through @Input or directly from an object property

In our main application, we utilize an RTF control that is standalone and generates CSS classes along with HTML using these classes. This content is loaded through an API.

The generated output looks like this:

.cs95E872D0{} .csCF6BBF71{font-weight:normal;font-style:normal;}

and the HTML is structured as follows:

<p class="cs95E872D0"><span class="csCF6BBF71">this is a test</span></p>

The formatting may not be ideal, but since we cannot modify how the CSS/HTML is generated, we have to work with it as is.

My goal is to display this HTML on a page (easily achievable using the [innerHTML] attribute), however, incorporating the CSS has proven to be challenging.

I attempted creating a new component:

import { Component, Input } from "@angular/core";

@Component({
    selector: 'htmlrender',
    template: `<span [innerHtml]="html"></span>`,
    styles: ['{{styles}}']
})
export class TestComponent {
    @Input() html: string;
    @Input() styles: string;
}

Nevertheless, it is rendered in this manner:

<htmlrender _ngcontent-c9="" _nghost-c11="" ng-reflect-styles=".cs95E872D0{} .csCF6BBF71{font">
    <span _ngcontent-c11=""></span>
</htmlrender>

This approach does not produce the desired outcome. I also experimented with placing the CSS within <style> tags, but unfortunately, that did not resolve the issue.

Answer №1

To achieve consistent output, utilize string.slice() and string.replace().

Here is an example of how it can be done:

this.text = '.cs95E872D0{} .csCF6BBF71{font-weight:normal;font-style:normal;}';
let sliced = this.text.split(".", 3);
console.log('.' + sliced[1]);
console.log('.' + sliced[2]);

The results from slice[1] and slice[2] are shown below:

.cs95E872D0{}

.csCF6BBF71{font-weight:normal;font-style:normal;}

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

Semicolon unexpectedly found in function in the Chrome Developer Tools

When I use Chrome developer tools, it tells me that there is an unexpected semicolon after the 3rd closing curly brace in this function. Shouldn't the semicolon be there to end the var form declaration? But if I remove it, Chrome says that the 4th cur ...

Transfer the div above to the other div

My dynamic set of divs have varying heights generated dynamically even though their widths are fixed. Despite setting them to float:left, they are not aligning perfectly under each other. The divs are forming rows with the height of the row being determine ...

Having an issue with displaying the country name and country code in a table using the Angular7 custom pipe

country code: "ab", "aa", "fr", ... I need to create a custom pipe that will convert a countryCode into a countryName, such as: "ab" → "Abkhazian", "ch" → "Chinese", "fr" ...

Error: Unexpected character < in node_modules/angular2/ts/package.json syntax

I am currently working on developing an app with Angular 2 and have encountered an error during setup. The error message I received is: SyntaxError: /home/mts/Desktop/sampleProject/appSails/node_modules/angular2/ts/package.json: Unexpected token < at O ...

Update constant data returned from Angular2 service

Issue at Hand: I am facing an issue where I want to store default data in a separate file so that I can easily reset the default value when needed. However, the Const Data seems to be getting updated. I could simply hardcode the value directly into the co ...

Unable to Achieve Full Height with Vuetify Container

Challenge: I'm facing an issue with my <v-container> component not consistently spanning the entire height of the application. Despite trying various methods such as using the fill-height property, setting height: 100%;, height: 100vh;, and expe ...

The jQuery click event does not fire within a bootstrap carousel

I am trying to set up a bootstrap carousel where clicking on an image inside it will trigger a self-made lightbox. However, I am facing some issues with the JavaScript code not being triggered with the following syntax: $('html').on("click", ".l ...

Splitting code efficiently using TypeScript and Webpack

Exploring the potential of webpack's code splitting feature to create separate bundles for my TypeScript app has been a priority. After scouring the web for solutions, I stumbled upon a possible lead here: https://github.com/TypeStrong/ts-loader/blob/ ...

Distance between two lines within a table cell is larger than expected after inserting an image - XHTML/CSS

I have a table created in XTMl, and within one of the cells I have two lines of text: Firstname Surname I want to add an image to the right of these lines. However, when I try to insert the image using <img>, there ends up being a gap between the t ...

Resolve the issue of autofill data overlapping with field labels

My form incorporates the Canada Post AddressComplete tool, which functions similarly to Google Maps Autocomplete. As you type your address, suggestions appear in a drop-down menu. However, after selecting an address, the text in the Postal Code and City f ...

Using Angular 2 with the @ngtools/webpack for Ahead of Time (AOT)

I'm attempting to implement AOT in Angular 2 using webpack and @ngtools/webpack. During compilation, I encounter no errors. However, upon opening the site in the browser, a console error occurs: No NgModule metadata found for 'AppModule' ...

Utilizing TypeScript for parameter typing in React Router's URLs

When trying to type URL parameters, I encountered an error stating "type {} is missing the following properties from type RouteComponentsProps<MatchParam,StaticContextn unkwon>: history, location, match. How do I correctly type URL parameters in Type ...

PHP code to generate HTML tables

Hey everyone, I'm new to php and I'm trying to create an HTML table string to use with mpdf. However, when I construct the table, only the header is showing up, not the data. Here is the code I'm using: $html ='<div class="row"> ...

The type '(params: any) => CSSProperties' does not share any properties with the type 'Properties<string | number>'. Perhaps you meant to invoke it?

Why isn't this property working in react CSS when it is of type CSSProperties? How can I make it work with Properties<string | number>? export const fields: GridFieldsConfiguration[] = [ { ...defaultColDefs, field: &a ...

What are some creative ways to format text displayed using ngx-markdown?

I'm currently in the process of building a blog using Angular and I have decided to use Markdown for creating my posts. Specifically, I am looking into integrating ngx-markdown for rendering purposes. As of now, I am developing a component dedicated t ...

Ways to ensure that an svg image is perfectly sized to its parent container

I'm exploring the svg <image> tag for the first time. I've decided to use this tag to apply a gray filter on an image (thanks, IE). Check out my HTML code below: <div class="container"> <div class="item"> <svg version ...

Allowing users to navigate through a fixed content page using a scrolling menu, all without the need

On my webpage, I have integrated Google Maps. The layout is divided into a menu on the left and content on the right. I am looking to make the left side (menu) scrollable, while the right side (content, which includes the map) remains static without scrol ...

Ajax unable to retrieve the value of a textarea

I am facing an issue with posting the value of a textarea input to the server using AJAX. The AJAX call itself is functioning correctly, but it does not seem to recognize the content inside the textarea. Here is my HTML setup: <div class="promptBody"& ...

Applying FadeIn Effect to Background Image on Hover

For several applications, I have utilized this jQuery code that swaps images with a smooth fading effect. It's incredibly straightforward. $(document).ready(function() { $("img.a").hover( function() { $(this).stop().animate({ ...

I am interested in incorporating a SVG with a unique blend mode on top of a video

Struggling to overlay a svg logo onto a video clip (mp4) using mix-blend-mode: difference in Shopware 6. Here is my CSS: #gr-logo svg path { mix-blend-mode: difference; } I've attempted with png files, applied blend modes to containers, and more. I ...