The malfunctioning collapse feature in Bootstrap 4 sidebar within an Angular 6 application

I am trying to find a way to collapse and reopen the sidebar when clicking on a button. I have attempted to create a function to achieve this, but unfortunately it did not work as expected.

Important: I need to collapse the sidebar without relying on jquery or javascript.

This is what I currently have:

https://stackblitz.com/edit/angular-n4x1br?file=src%2Fapp%2Fsidebar%2Fsidebar.component.html

Answer №1

The ngClass directive in your code is applying the show class, but you actually want to apply the active class instead.

<nav id="sidebar" [ngClass]="{ 'active': navbarOpen }">

Check out this StackBlitz link for reference

Answer №2

Start by creating a boolean variable named 'collapsed' set to true. When you click on a button, it toggles between true and false, changing the style of your HTML according to the variable's value. Good luck!

This code belongs in your HTML:

<div class="sidenav" [ngClass]="screen.collapsed ? 'sidenav-collapsed': ''">

This function should go in your TypeScript file:

togglecollapse(): void{ 
this.screen.collapsed = !this.screen.collapsed; 

}

When you call this function with a button click, it will update the variable and adjust the CSS accordingly.

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

HTML: Checkbox not responding to unchecked value... malfunctioning

I am encountering an issue with setting the Checked / Unchecked value of a checkbox. The problem I'm facing is that when the checkbox is unchecked, it returns an empty string. Any thoughts on how to solve this? Here's what I have done so far: ...

Do we require two-way binding to effectively exchange data between components in this situation?

I'm currently facing some challenges in updating data in a child component when new data is added from the parent component. Here's a snippet of my template: <ion-content> <app-feed-comments [comments]="comments" [userId]=& ...

Creating a Button with Icon and Text in TypeScript: A step-by-step guide

I attempted to create a button with both text and an icon. Initially, I tried doing it in HTML. <button> <img src="img/favicon.png" alt="Image" width="30px" height="30px" > Button Text ...

Limiting the display of every item in Angular ngFor

I'm currently working with Angular and I have the following code in my component.html: <div class="card" *ngFor="let item of galleries;" (mouseenter)=" setBackground(item?.image?.fullpath)" (mouseover)="showCount ...

Optimizing Angular for search engines: step-by-step guide

Regarding Angular SEO, I have a question about setting meta tags in the constructors of .ts files. I have implemented the following code: //To set the page title this.titleServ.setTitle("PAGE TITLE") //To set the meta description this.meta.addTag ...

What is causing TypeScript to compile and remove local variables in my Angular base controller?

I am trying to develop a base controller in Typescript/Angular for accessing form data, but I'm encountering an issue where the form member seems to be getting removed during compilation and is not present in the generated JavaScript code. Could you ...

Angular directive to delete the last character when a change is made via ngModel

I have 2 input fields where I enter a value and concatenate them into a new one. Here is the HTML code: <div class="form-group"> <label>{{l("FirstName")}}</label> <input #firstNameInput="ngMode ...

Navigational menu routing with AngularJS2 using router link paths

Currently, I am working on developing a navigation menu using angularJS2. Here is the snippet from my app.component.ts: import {provide, Component} from 'angular2/core'; import {APP_BASE_HREF, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, HashLocati ...

Unable to test the subscribe functionality in Angular

There is a subscribe method in my ts file within ngAfterViewInit() that is not triggering as expected during testing and debugging. I need to manually set mock data inside the subscribe method for testing purposes. ts file import mockData from 'test ...

Angular 5 Service Unit Testing for UPDATE Function

Currently, I am implementing a stepper feature with back, step, next steps. On the last step, when the user clicks 'done,' I need to call a service to update user data. While I have successfully tested the backStep() and nextStep() methods, I now ...

Bypass Auth0 HttpInterceptor based on certain conditions

During the transition from Firebase to Auth0, my Angular front-end application authenticates users to either Firebase or Auth0 based on their email address. I am working on configuring the Auth0 AuthHttpInterceptor provided in the Auth0 Angular SDK for SPA ...

What is the proper way to correctly populate an input field with an empty value of ""?

I am having an issue where putting value="" inside the input is preventing me from filling in the username and password fields. When I remove the value="", I can fill in the fields, but with value="" present, nothing happens. {/* User */} <div class=" ...

Issue with Moment Js: Unable to change date with time zone function

Trying to convert a UTC date and time to local time ("Europe/Paris") using moment's timezone function, but encountering issues. The code I am using is: var m = moment.tz(this.startTime, 'Europe/Paris'); this.startTime = m.format("YYYY-MM-DD ...

Exploring the utilization of CSS host selectors with Angular4 to target direct child elements

Within my app.component.css file, I currently have the following code snippet: * ~ * { margin-top: 24px; } This rule actually adds some top margin to all elements that come after the first one. However, this is not exactly what I intend to achieve. My ...

Tips on sending a list via @Input using Angular5

I was seeking guidance on how to pass a list through an input. In my parent component, I have the following: list: Hero[] = [{Id: 2, Name: "Sll"}, {Id: 3, Name: "Sldsadasd"}] and in the HTML: <app-add-hero list="{{list}}" hero={{hero}}></app-ad ...

The Angular 2.0 HTTP post request encountered an error when trying to respond with a status of 200 Ok for a null

I'm currently attempting to post data using http in Angular 2. I have added an http call with the corresponding API, and when I click the button, it should send data to my database. However, an error is showing up in the console. XMLHttpRequest canno ...

Challenges arise when working with Vue 3.2 using the <script setup> tag in conjunction with TypeScript type

Hey there! I've been working with the Vue 3.2 <script setup> tag along with TypeScript. In a simple scenario, I'm aiming to display a user ID in the template. Technically, my code is functioning correctly as it shows the user ID as expect ...

How can we enhance the angular material date picker by inserting a div on either the left or right side to display

I'm looking to customize the style of an angular material datepicker and include a box that displays the selected date, similar to the image shown here: enter image description here Does anyone have suggestions on how to achieve this? Any assistance ...

Tips for updating the styles within a class for Angular 6 dynamically

Currently, I am able to update the button design using ng-container. Here is a snippet of the code: <ng-container *ngIf="isDisabled;"> <button class="bot-btn-disabled" (click)="confirm()" [disabled]=this. ...

Turn off the interconnected route while utilizing npm to display the tree of dependencies

If I want to display the project's dependencies tree using npm, I would use the following command: npm ls lodash The output will look something like this: > npm ls lodash npm info using [email protected] npm info using [email protected] ...