Implementing click event binding with various CSS styles in Angular

Is there a way to attach multiple css classes to the click event of the Submit button in Angular? I want the style to change when the button is clicked.

HTML

<div class="mainbody" [ngClass]="getStyle">
    <button (click)="getStyle= !getStyle">Submit</button>
</div>

TypeScript

export class AppComponent {
  style1: boolean = true;
  style2: boolean = true;
}

getStyle()

Answer №1

You have the option to define two distinct state objects:

export class AppComponent {
  initialState = {
    styleA: true,
    styleB: true
  }
  finalState = {
    styleA: false,
    styleB: false
  }
  isActive = false;
}

HTML

<div class="main-content" [ngClass]="isActive ? finalState : initialState">
  <button (click)="isActive = !isActive">Click Here</button>
</div>

Answer №2

If you're looking to change the style based on whether getStyle is true or false, here's how you can achieve that:

Here's an example of how you can implement it:

<div class="content" [ngClass]="{'style_1': getStyle, 'style_2': !getStyle}">
  <button (click)="getStyle = !getStyle">Toggle Style</button>
</div>

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

CDK Drag and Drop capability for lists within lists

I am trying to figure out how to display users and their corresponding information in a structured way. Each user should be presented in their own column, with the associated information displayed within that column. I have been attempting to drag and drop ...

"Customized Bootstrap button featuring a tooltip and modal, experiencing incorrect padding within the button group design

I encountered an issue with a button-group containing 2 buttons, each with a tooltip and a modal. Bootstrap recommends wrapping the buttons with spans to manage two "data-bs-toggle" attributes, which is what I initially did. While the functionality works ...

What is the process of integrating SCSS into an Angular2 Seed Project?

Is there a recommended method for incorporating SCSS into an Angular2 Seed Project? Are there any informative tutorials or reference sites available? I attempted to implement SCSS following instructions from this link https://github.com/AngularClass/angu ...

How to effectively handle null in Typescript when accessing types with index signatures unsafely

Why am I getting an error that test might be potentially undefined even though I've enabled strictNullCheck in my tsconfig.json file? (I'm unsure of the keys beforehand) const a: Record<string, {value: string}> = {} a["test"].va ...

Encountering unspecified values when subscribing to a BehaviorSubject and receiving it as an Observable

My goal is to display the name of the currently logged-in user in the header of my application. However, I encountered an issue where upon refreshing the page, the value would be lost due to SPA behavior (even though the data is stored in local storage). T ...

utilize a document within ngx Translate

I've been working on transitioning an app from AngularJS to Angular 4. As I gradually move components over, I'm encountering issues with translation. My goal is to continue using the old AngularJS translation alongside ngx-translate. Prior to ...

Make the child DIV's width equal to the width of its parent DIV, even when the parent DIV's width is not

I am experiencing an issue with a dropdown menu. I have created this code (with the assistance of w3schools)... :) The problem is that the sub-division that appears when I hover over a division does not match the width of the parent. I attempted to assig ...

What are some ways to customize the appearance of Stripe credit card form fields?

I've successfully implemented stripe using this HTML code and Bootstrap 4. <div class="form-control" id="card-element"></div> As a result, I get the displayed output: https://i.sstatic.net/Aw653.png However, I am loo ...

Encountered an npm install error: The package does not meet the peerDependencies requirements of its siblings

When I try to execute the npm install command, it throws these errors: npm ERR! peerinvalid The package @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0b3bfa2b590e7fee2fee1e5">[email protected]</a> does ...

Integrating variable interpolation within media queries using Compass/Sass

Is there a method to achieve the desired result in Compass/Sass? $padding: 3em div { padding: $padding; } @media screen and (max-width: 780px) { $padding: 2em; } /* Result: div { padding: 3em; } @media screen and (max-width: 780px) { ...

Unable to locate angular moment components

I encountered an error while trying to build my angular project using npm start. I attempted to install dependencies using the yarn command, but it didn't solve the issue. Below is the error message that I am receiving: ERROR in ./src/AppPreBootstra ...

Turn off dropdown menu animation (JavaScript)

I am trying to completely disable the transition effect on my drop-down menu. Below is the JavaScript code that I believe needs to be changed: function(e) { if ((/input|textarea/i.test(e.target.tagName) ? !(32 === e.which || 27 !== e.which && (40 !== e ...

Angular2 URL fragment not recognized in Firefox

My website's main page is divided into different sections: Introduction Services Projects All the content is on the same page. To navigate to a specific section, I use Angular2 fragments in my navigation structure. Here's the HTML snippet for ...

Is there a way to prevent this <li> tag from collapsing?

https://i.sstatic.net/8ulvX.png Yellow is currently the color of <li> elements that are children of <ul>. However, if I apply either a float or display property to the <li> or <ul>, the appearance changes. https://i.sstatic.net/lP ...

Struggling to bring in components in ReactJS

My journey with ReactJS has just begun, and I've encountered some issues with the code that I believe should work but doesn't. To start off, I set up a new ReactJS project using the npm command create-react-app. Following this, I installed Googl ...

Having trouble aligning modal to the left in Bootstrap 4 and MVC 5

I am currently working on a project where I am using a modal window to display an iframe. The webpage within the iframe is quite wide, so I have adjusted the width accordingly. However, the modal itself is situated in the center of the screen, causing the ...

Unraveling the mysteries of webpack configuration

import * as webpack from 'webpack'; ... transforms.webpackConfiguration = (config: webpack.Configuration) => { patchWebpackConfig(config, options); While reviewing code within an Angular project, I came across the snippet above. One part ...

Dynamically obtaining the screen width in JSP using a variable

Could someone provide assistance on how to resize an image using an img src="blah.jpg?width=x" so that my page can be displayed at various sizes? I just need x (width) as a JSP variable. Update 2021: It's been 9 years since this question wa ...

In Rxjs, ConcatMap doesn't get executed

I am currently developing a file upload component that involves checking for the existence of a file before proceeding with the upload process. If the file already exists, the user is given the option to either save as a copy or overwrite the existing file ...

Strategies for adjusting text size to fit within a resizable div container

I'm facing an issue with a resizable div that has text inside. When I resize the div, the last line of the text goes beyond the boundaries of the div. How can I ensure that all the text fits within the resizable div while maintaining the appropriate f ...