What is the method for applying a class using brackets in Angular?

Can we achieve something like the example below?

<div *ngFor="let post of posts">
  <p class="quote {{post.color}}-quote">
    {{ post.content }}
    <span class="author">Anatoly Weinstein, Founder of Theora</span>
  </p>
</div>

Key line:

class="quote {{post.color}}-quote"

I've come across similar concerns, but I'd prefer to avoid using booleans and hardcoding each element.

Answer №1

Utilizing the template binding with brackets may not necessarily offer any distinct advantages, as the same functionality can be achieved using the ngClass directive:

<span class="highlighted-text" [ngClass]="[quote.style+'-text']">

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

To identify the dynamically inserted and generated div element amidst the other elements on the page

My goal is to have a new div generated and inserted over the other elements on the page when a button is clicked. Here is the markup I've written: <div id="parent"> <div id="d1"> </div> <div id="d2"> </div&g ...

CSS Global Class with a Text Shadow Override

Currently, my text shadow is being overridden throughout my HTML / CSS3 Document. The following tags are causing the issue: html *, #footerTextdetail { text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.7), 2px 2px 2px (0, 0, 0, 0.3) !important; } I am struggli ...

Change the position of an array element when displayed on an HTML page

I'm trying to figure out a way to manipulate the position of an HTML element that is stored inside an array. The issue I am facing is that my code generates a new div every time a specific function is called, and this div has a class applied to it for ...

Creating links within a single image in HTML?

Is there a way to hyperlink different parts of a single image to various webpages using JavaScript coordinates or any other method? ...

Is there a way to trigger a refresh of an Angular HttpClient observable based on changes in another observable?

My main goal is to implement a set of filters in the page header that will control input parameters for various analytics pages within my app. I have created an Angular service that encapsulates the filter functionality and exposes an observable that emits ...

Angular 6 seems to be having issues loading Bootstrap

I've run into an issue while trying to incorporate Bootstrap into Angular 6. I have already installed Bootstrap using npm install bootstrap and added it to angular.json, but it's still not functioning. Is there something else I should be doing? A ...

Tips on how to link a local absolute file path within HTML code

I am currently in the process of building a website using Apache, MySQL, and PHP on my local machine. Within my project, I have organized different subfolders and I am facing an issue with giving an absolute reference to my css file from my main header.php ...

What could be causing the drop-down menu to function properly when offline but not when accessed on the server?

When visiting the website here and clicking on the contact link, you should see a Google map. However, for some unknown reason, it does not display. The strange part is that when I open the contact.html file directly, the map works perfectly fine. It seem ...

Designing Navigation Layouts

Visit my Wordpress site The navigation bar features a unique two-layer image hover effect where the color changes from black to white on hover. The images are stored within the .hoveringnav section with classes "top" for the top white image and "bottom" ...

Dragging elements in a scrollable div with JQueryUI causes the dragged item to disappear

I am facing an issue while working with JQuery UI-Draggable divs inside a container that has a fixed height and a scrollable y-axis. Whenever I drag the elements outside the container, the overflow-y: scroll rule hides them. You can view the code on jsFid ...

Having Trouble Getting a New Line with CSS - Why Won't Display: Block Work?

I'm currently facing an issue where I want a div to occupy a full line and force the next div onto a new line. This is typically achieved by using display:block in the CSS for the first div, or so I believe. However, this approach doesn't seem to ...

jQuery, the magical tool that can make forms disappear and reappear in a blink

As the heading suggests, here is the code I attempted. The forms are designed to be displayed one after the other, with each subsequent form's appearance being determined by the information provided in the previous forms. $(document).ready(function() ...

The mat table is not displaying the values from the API despite receiving the correct array

I recently made the decision to dive into learning Angular, but I've encountered some difficulties. Specifically, I'm struggling to populate a table with values retrieved from an API using MatTable. Despite receiving the correct values from the A ...

Importing a third-party JavaScript library in Angular 2 RC5: Utilizing Showdown Library

Struggling with the integration of Showdown as a vendor in my project. Whenever I compile, the browser console throws an error saying showdown is not defined. Since it's a vendor package, importing it directly into app.module.ts doesn't seem to b ...

"Duration parameter in jQuery animate() function not working as expected in Firefox

Just starting out with jQuery and I have a class for absolute centering (it's working as intended): .Absolute-Center { margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; } #logo { background: url(logo.png) no-rep ...

Encountering issues accessing the key value 'templateUrl' in Angular6

I have an object in my component.ts file that I need to iterate through in the HTML template using a ui-comp-menu element. menuObject = [{ 'labels': 'Content1', 'templateUrl': 'assets/partials/sample.html ...

Steps to avoid reinitializing the component upon changing routes in an Angular 9 component

In my component, the width of a chart is stored in a variable (because I can't use style for d3). However, every time the route changes, all variables in this class component become undefined. I have tried using ngIf, services (which also become unde ...

Array containing multiple service providers in Angular

I encountered a problem while utilizing multiple providers in my application: ERROR Error: No provider for Array! at injectionError (VM634 core.umd.js:1238) [angular] at noProviderError (VM634 core.umd.js:1276) [angular] at ReflectiveInjector_._throwOrNul ...

Alter jqGrid appearance

Is it possible to enhance the appearance of a jqGrid 3.5 grid and make it visually appealing by utilizing jQuery, custom CSS, or any other method? ...

Adjusting the size of the image based on the content with a background-size cover feature

I've been spending days trying to figure out the best way to properly display a background image on various screen sizes. Check out this working example at . You can also view it on JSFiddle for better clarity. The issue I'm facing is as follow ...