Using Angular 6 to apply an extra class based on a true boolean value

I am currently working on setting up a voting feature where the active vote is highlighted with a blue background. To achieve this, I am using 2 boolean variables to track the score and the selected option. While attempting to use NgClass, I encountered some difficulty in understanding how it functions in my specific scenario as I need to add an extra class to the existing 2 classes.

Styling (CSS)

.voting {
    cursor: pointer; 
    display: inline;
}
.activeVote {
    background: #2980b9;
}

HTML Code

The 'voting' class and 'noSelect' class should always remain active. However, when clicking on 'increaseUpvote()', the 'activeVote' class should be added to indicate that the upvoted boolean is true. The same behavior should apply to the downvote option.

<p style="font-size: 28px;">{{post.upvotes}}
    <mat-icon class="voting noSelect" (click)="increaseUpvote()">arrow_upward
    </mat-icon>
    <mat-icon class="voting noSelect" (click)="increaseDownvote()">
        arrow_downward
    </mat-icon>
</p>

Typescript Code

public upvoted: boolean = false;
public downvoted: boolean = false;

increaseUpvote(): void {
    if (this.downvoted) {
        this.post.upvote();
        this.downvoted = false;
    }
    if (!this.upvoted) {
        this.post.upvote();
        this.upvoted = true;
    } else if (this.upvoted) {
        this.post.removeUpvote();
        this.upvoted = false;
    }
}

increaseDownvote(): void {
    if (this.upvoted) {
        this.post.removeUpvote();
        this.upvoted = false;
    }
    if (!this.downvoted) {
        this.post.removeUpvote();
        this.downvoted = true;
    } else if (this.downvoted) {
        this.post.upvote();
        this.downvoted = false;
    }
}

Answer №1

If you're searching for a solution, Angular's basic template syntax might have what you need.

To apply the "activeVote" class when "upvoted" is true, use the following:

<div [class.activeVote]="upvoted">

Alternatively, to add the "activeVote" class when "upvoted" is false, try this:

<div [class.activeVote]="!upvoted">

For further information, visit https://angular.io/guide/template-syntax#binding-targets

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

Creating a Bootstrap column that is the height of two rows

Trying to create a calculator and have achieved perfect alignment for most elements, but struggling to fit in one last button. I need this button to be the height of two rows, but adjusting its height directly causes issues with rows below. https://i.ssta ...

Scrolling to the next wrap class in jQuery is not functioning properly with the chatbox and ScrollTop

I have created a chat box using jQuery. However, I am facing an issue where when I click the button multiple times, the scroll returns to the top. My objective is for the scroll to move to the next wrap class when the button is clicked. Here is a snippet ...

Tips for effectively incorporating CSS classes with Booststrap and Wagtail

Currently, I am utilizing a Bootstrap theme and trying to place up to three cards horizontally on a page at one specific location. Here is the HTML code: <div class="container"> <h1 class="text-center">{{ self.title }}</h1> <d ...

Is it possible to make a DIV adjust its size automatically when the browser is resized?

Greetings! Here's my initial query, with more to follow. I've encountered an issue where a div on my page is set to 70% of the screen, but upon resizing the browser to a smaller size, some content within the div extends past the bottom of the pa ...

Use CSS3 to conceal the form while displaying the default div

In my project, I am restricted to using only HTML5 and CSS3. Currently, I have a form that requires simulating the action of "sending form data". Upon clicking the submit button, the form should be hidden and a default div (#result) needs to be displayed. ...

Cover any HTML element with a translucent overlay box

I have a unique problem with an HTML file that is out of my control when it comes to its content. My only option is to inject a CSS file and/or JavaScript (potentially using libraries like jQuery) into the mix. Within this HTML, there are elements that re ...

The background image fails to fill the entire page

I am facing a strange issue with a website. The background image does not cover the entire page, leaving large white sections when I resize the window. Oddly enough, this problem only occurs on certain PCs or laptops. After asking several friends to test t ...

Struggling with formatting images to ensure consistency in size using HTML and CSS

Currently, as a novice in HTML and CSS, I am encountering image misalignment issues while attempting to make them uniform in size. How can this be rectified? The existing HTML code is shown below: <div class="container text-center"> <h3> ...

Tips for removing padding from a container-fluid in bootstrap 4 without triggering horizontal scroll bars

The container-fluid class from Bootstrap adds a padding of 15px to the left and right of its contents. However, when I try to create a separate class to remove this padding and make the content cover the entire width without any space on either side, it ...

Design of multiple divs with asymmetrical hover effects

Consider the following scenario: The gallery-menu consists of 2 title-images and 7 sub-images. 3 sub-images are connected to title-image 1, while 4 sub-images are linked to title-image 2. The sub-images are not in order corresponding to their titles. All ...

Loading Kendo JS on the client side proves to be rather time-consuming

Working on my project, I have encountered a problem with the kendo ui scheduler where the downloading of the kendo js and css files on the client side is causing slowness on our website. To address this issue, we are attempting to download the kendo js and ...

The 3D card flipping animation is dragging on for an eternity

I'm experiencing a delay in triggering my 3D Card Flip animation on click, and I can't figure out why it's happening. If you want to see an example, please scroll down and click on any black and white picture here: Here is the code I' ...

Achieving a full-height div using CSS and HTML to fill the remaining space

Here is the current layout structure I am working with: div { border: 1px solid } <div id="col_1" style="float:left;width:150px;">1</div> <div id="col_2" style="float:left;width:100px;">2</div> <div id="col_3" style="float:l ...

Is there a way to customize the CSS ul menu specifically for the JavaScript autocomplete feature?

I have created a search page with autocomplete for the first textbox, but I noticed that the ul/li CSS classes used for styling the main menu are impacting the JavaScript list. How can I override the menu styling to display a regular list format? Being a ...

Learn the steps to successfully rotate the custom icon in Material-Ui select without impacting its functionality and making sure it remains clickable

I've been trying to incorporate my own icon for the material UI select component. I successfully replaced the default icon using the "IconComponent" attribute in MU select. However, I'm facing issues with the new icon not rotating when the menu ...

Has the tr:active selector in IE 11 malfunctioned?

My issue is with selecting and highlighting td elements in IE. I am unable to highlight all td elements in a tr using tr:active, although this works correctly in FireFox and Chrome. You can view an example on JsFiddle here. Can someone help me identify if ...

"Upon the page loading, a flickering effect can be seen in the div that

I'm trying to avoid this issue. Whenever I use ng-repeat, it initially displays everything inside the div along with empty scopes for a brief moment before loading the data and showing it normally. I want to eliminate this flicker upon page load but c ...

Tips for overlaying text on an image in html with the ability to zoom in/out and adjust resolution

My challenge is aligning text over an image so that they move together when zooming in or out. However, I am facing difficulties as the text and image seem to move in different directions. I have attempted using media queries and adjusting the positions of ...

Aligning the content in the center of a div with inline display

Hi there, I have a quick question for the experts out there. I'm new to all of this so please bear with me :) I'm attempting to center an inline social sharing div on my website, but it's proving to be quite challenging. The div is nested w ...

Enhancing a website design for touchscreen devices to address issues like unintentional mouse dragging

Currently, I am in the process of creating a compact web-based kiosk interface specifically designed for a 7-inch touchscreen. The details of the system itself are not pertinent at this time; however, it is worth noting that the browser running on the touc ...