Numerous toggle classes available

Having the following HTML inside a <span> element:

<span (click)="openLeft()"></span>

A method in a @Component sets a boolean variable like so:

private isOpen: boolean;
openLeft() {
    this.isOpen = !this.isOpen;
}

To toggle classes on another element similar to jQuery, we need to do:

$('.collapse').toggleClass('in').toggleClass('hidden-xs').toggleClass('visible-xs');

We want these classes added or removed based on the boolean variable value.

How can this task be accomplished?

Answer №1

[ngClass]="{'collapse': isOpen, 'hidden-xs': isOpen, 'visible-xs': isOpen}"

alternatively

[ngClass]="isOpen ? ['collapse', 'hidden-xs', 'visible-xs'] : []"

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

Leveraging symbols as object key type in TypeScript

I am attempting to create an object with a symbol as the key type, following MDN's guidance: A symbol value may be used as an identifier for object properties [...] However, when trying to use it as the key property type: type obj = { [key: s ...

Diving into Angular 2 RC6: Understanding the Differences Between Modules and Components

I'm feeling a bit overwhelmed with RC6 of angular2. I'm struggling to figure out how to adjust my code with modules and components and I'm confused about the distinctions between the two. Can you assist me in incorporating directives, provid ...

Angular is unable to create a new project

I attempted to create a new project using AngularCLI in the following way: ng new my-app-ambition However, this resulted in the following errors: npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning UNABLE_TO_VER ...

Issue encountered while managing login error messages: http://localhost:3000/auth/login net::ERR_ABORTED 405 (Method Not Allowed)

I am working on the /app/auth/login/route.ts file. import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs' import { cookies } from 'next/headers' import { NextResponse } from 'next/server' export async functi ...

Breaking down types in Typescript: Extracting individual types from an object containing multiple objects

Having a query: const queries = { light: { a... b... }, dark: { a... b... c... d... }, The react element requires a colors parameter that corresponds to one of the themes in the above object, with each theme containing a un ...

Issue: The initial parameter should be a File or Blob object

Hey there! I'm currently utilizing the compressorjs plugin for compressing images, but I'm encountering an issue when selecting images. You can find out more about the plugin here. Here is my code snippet: window.resolveLocalFileSystemURL( ...

Global Inertia Headers

How can I ensure that a custom header (Accept-Content-Language) is sent with every request, including Inertia manual visits? Below is the code snippet where I define and set the header: import axios from 'axios'; const lang = localStorage.getIt ...

Save solely the timing information in Mongodb

Looking for advice on storing time values in MongoDB? Users will be inputting times as strings, such as "05:20", and you need to convert and store this data correctly. Any suggestions on how to achieve this? I've attempted using the Date object with ...

deliver a promise with a function's value

I have created a function to convert a file to base64 for displaying the file. ConvertFileToAddress(event): string { let localAddress: any; const reader = new FileReader(); reader.readAsDataURL(event.target['files'][0]); reader ...

Issue with accessing undefined property in Angular 2+ using Typescript

In my Angular 7 project, I am retrieving data from a service which looks like this: {name: "peter", datetime: 1557996975991} I have a method that is supposed to retrieve this data: myMethod() { this.myService.getdata().subscribe((res) = ...

Using Generic Types in TypeScript for Conditional Logic

To better illustrate my goal, I will use code: Let's start with two classes: Shoe and Dress class Shoe { constructor(public size: number){} } class Dress { constructor(public style: string){} } I need a generic box that can hold either a ...

“What is the process of setting a referenced object to null?”

Here is an example of the code I'm working with: ngOnInit{ let p1 : Person = {}; console.log(p1); //Object { } this.setNull<Person>(p1); console.log(p1); //Object { } } private setNull<T>(obj : T){ obj = null; } My objective is to ...

Use leaflet.js in next js to conceal the remainder of the map surrounding the country

I'm currently facing an issue and would appreciate some assistance. My objective is to display only the map of Cameroon while hiding the other maps. I am utilizing Leaflet in conjunction with Next.js to showcase the map. I came across a helpful page R ...

How can React TypeScript bind an array to routes effectively?

In my project, I am using the standard VisualStudio 2017 ASP.NET Core 2.0 React Template. There is a class Home included in the template: import { RouteComponentProps } from 'react-router'; export class Home extends React.Component<Rout ...

The Angular 2 view will remain unchanged until the user interacts with a different input box

I am currently working on implementing form validation using Reactive Forms in Angular 2. Here is the scenario: There are two input fields Here are image examples for step 1 and step 2: https://i.stack.imgur.com/nZlkk.png https://i.stack.imgur.com/jNIFj ...

When moving to a different route inside the router.events function, the browser becomes unresponsive

Within my top navbar component, I have set up a subscription to the router events: this.router.events.subscribe(event => { if (event instanceof RoutesRecognized && this.authService.isLoggedIn()) { // additional custom logic here: ...

The distinctUntilChanged function encounters an issue when no comparison function is given

considering the following code: /** * Generating a stream of car objects from an array. */ from([ { name: 'Porsche', model: '911' }, { name: 'Porsche', model: '911' }, { name: 'Ferrari', model: &apo ...

Utilizing NPM Workspace Project in conjunction with Vite to eliminate the necessity of the dist folder during the Vite build process

I am currently working on a project that involves a module using full path exports instead of index files. This project is divided into 2 NPM workspaces: one called core and the other called examples. The challenge I am facing is avoiding long import pat ...

What is the preferred method for validating an Angular form - ng-model or form input name?

When it comes to validating an input field and providing feedback, there are two methods that I have noticed: <form name="myform" ng-submit="myform && myFunc()"> <input name="foo" ng-model="foo" ...

What causes RxJS concat to generate distinct values when given an array as input instead of separate arguments?

According to the documentation for rxjs, the concat operator can accept individual observables as arguments or an array of observables. When using individual arguments, the concatenated observable behaves as expected, combining values in sequence. Here&apo ...