Issue with the close button on ngb-alert not functioning properly

As I develop my website, I have incorporated an ngb-alert component to display an alert message to users upon login. While the alert itself is functioning properly, I have encountered an issue with the close button being misaligned, and I am struggling to troubleshoot it.

Below are snippets of the code I am currently working with:

**HTML COMPONENT**

<span>
    <ngb-alert type="primary" (close)="close()" [dismissible]="dismissible"> {{errorMessage}}</ngb-alert>
</span>


**TYPESCRIPT COMPONENT**

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-ngbd-error-message',
  templateUrl: './error-message.component.html',
  styleUrls: ['./error-message.component.less']
})

export class NgbdErrorMessageComponent {
  @Input() errorMessage: string;
  @Input() close: () => void;
  @Input() dismissible = true;
}

**LESS STYLING**
@import url('../../../global-less/color-variables.less');

:host {
  width: 100%;
  height: 100%;
}
:host .alert {
  margin: 0;
}
:host .alert-dismissible .close {
  padding: .6rem 1rem;
}

The class assigned to the button within the ngb-alert appears to be btn-close. Here is the HTML element rendered on the webpage:

<ngb-alert _ngcontent-ng-c2763413438="" role="alert" type="primary" ng-reflect-type="primary" ng-reflect-dismissible="true" class="alert alert-primary show fade alert-dismissible">Amidst a cascade of sparkling emerald leaves, the whimsical hedgehog orchestrated a symphony with a violin.<button type="button" aria-label="Close" class="btn-close"></button><!--bindings={ "ng-reflect-ng-if": "true" }--></ngb-alert>

Here are the applied styles in sequence:

First Image of Styles

Second Image of Styles

See how it appears on the actual webpage:

Screenshot of Component

My current setup includes:

  • Angular - 16.2.2

  • NGBootstrap - 15.0.1

I have investigated possible reasons for the issue, such as:

  1. Conflicting Styles
  2. Incompatible ngbootstrap Version
  3. Missing Dependencies

Answer №1

The term "Output" in ngb-alert is shown as "closed" instead of "close"

<ngb-alert type="primary"
           (closed)="close()"
           [dismissible]="dismissible">
   {{errorMessage}}
</ngb-alert>

By the way, you can utilize an @Input in your parent component like this:

//In parent
    <app-ngbd-error-message errorMessage='hi, error' [close]="close">
       </app-ngbd-error-message>

  close()
  {
    console.log("hi")
  }

You have the option to use @Output in your component:

//Your component
  @Output() close:EventEmitter<void>=new EventEmitter<void>()

<ngb-alert type="primary" (closed)="close.emit()" [dismissible]="dismissible"> 
           {{errorMessage}}
</ngb-alert>

//And in your parent

<app-ngbd-error-message errorMessage='hi, error' 
       (close)="close()">
</app-ngbd-error-message>

Remember: Ensure that you include the bootstrap.min.css in your .css file

Here is a stackblitz link

Update: The close button should have the class "btn-close", as defined in boostrap.min.css. You can also find this information on the Bootstrap website.

.btn-close:hover {
    color: #000;
    text-decoration: none;
    opacity: .75;
}
.alert-dismissible .btn-close {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 2;
    padding: 1.25rem 1rem;
}
.btn-close {
    box-sizing: content-box;
    width: 1em;
    height: 1em;
    padding: .25em;
    color: #000;
    background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23000'%3e%3cpath d='M.293.293a1 1 0 0 1 1.414 0L8 6.586 14.293.293a1 1 0 1 1 1.414 1.414L9.414 8l6.293 6.293a1 1 0 0 1-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L6.586 8 .293 1.707a1 1 0 0 1 0-1.414z'/%3e%3c/svg%3e") center/1em auto no-repeat;
    border: 0;
    border-radius: .375rem;
    opacity: .5;
}

Answer №2

After upgrading the Bootstrap version in my project, I was able to solve the issues I was facing. It turns out that the discrepancies between my ng-bootstrap and Bootstrap versions were causing conflicts.

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

Tips for importing bootstrap scss efficiently in an angular project to prevent style duplication

Is there a way to optimize an Angular project for utilizing Bootstrap's mixins and variables without constantly importing the styles in every component? Currently, the project I'm working on imports the variables.scss file multiple times due to u ...

Angular: Marking individuals in a FormArray as labels or flags

I need to distinguish some members (FormControl / FormGroup) within a FormArray by labeling or flagging them to group accordingly. For instance: A user can input values in three ways (uploading an Excel file, generating from a back-end service, or manua ...

Error: scrollreveal JavaScript is not properly defined

Desperately seeking guidance on a particular code snippet... window.sr = ScrollReveal({ reset: true }); sr.reveal('.whitecircle, .circleStatsItemBox, .circleStat', { duration: 200 }); function circle_program() { var divElement = $(&apo ...

The custom color override feature in Bootstrap is not being applied as expected

I attempted to incorporate a new theme color into Bootstrap's color scheme, but encountered difficulties in getting it to work. To guide me through the process, I referred to this YouTube tutorial as well as the official Bootstrap documentation. Des ...

ngIf not working properly with the updated value of [(ngModel)]

I am encountering an issue with a select element that has options. The select is using the [(ngModel)] directive to save selected values into "rightFieldTypeId." I have elements that should be displayed based on the value of "rightFieldTypeId." Even though ...

Verify all inputs are complete in the FullScreenForm

I have been working on implementing a form from this website: http://tympanus.net/Development/FullscreenForm/ My main objective is to validate each input when the form is submitted and display any errors that may arise. To achieve this, I have already se ...

Easiest Angular Carousel Solution

My goal is to create a basic Angular Carousel to enhance my understanding of Angular. While I have received helpful answers in the past, I am seeking further clarification to deepen my knowledge of Angular2+ and Typescript. Here's what I have so far: ...

Query the Firebase database in Angular2 to locate the latitude and longitude values that are nearest to the user's current coordinates

I am working with a database table named locations, which contains a list of places along with their corresponding lat/long coordinates. Additionally, I am utilizing geolocation to retrieve the user's current lat/long. My goal is to identify the loc ...

Using line breaks and horizontal scrolling in HTML and CSS

I am struggling with a div that contains multiple small spans. My goal is to implement a horizontal scrollbar that extends up until the line break tags (a-z on each line) in order to view the full content on narrow screens. Although I have applied the &ap ...

Does catching errors cause the for loop to stop in JavaScript?

for (let index = 0, total = IDs.length; index < total; index++) { item = IDs[index]; hasError = false; try{ let id = db.getSiblingDB("door").jhi_user.findOne({"_id" : item}); } catch (error){ failedIDs.push(item); ...

Ways to identify and differentiate user clicks on various buttons

I have generated 3 different plan options from an array of objects retrieved from the backend. Depending on whether the plan is cheaper, the user's subscription, the corresponding button will display "downgrade", more expensive, the button will show ...

What type of JavaScript scope is accessible by an anchor tag's href attribute?

My current query involves using an <a> tag to invoke a JavaScript function: <a href="javascript:doSomething();">link</a> In which scope does this JS function need to be defined in order to be reachable? Is declaring it globally necessar ...

Implement the same reference functionality in a React Function component that is seen in a React Class component

Is it possible to replicate the same functionality of class component ref in a function component? Here's an example: const AnotherComponent = () => { const DoSomething = () => console.log(something); return <div> There is some co ...

There is zero gutter in Bootstrap

Is it possible to use the bootstrap grid system without any gutters? I want to have 3 columns in one row like this: http://prntscr.com/6gpmhm. I have shared the markup for the CSS I am using, which is the default CSS of the framework. <section class ...

Inserting multiple rows of data into a MySQL database in a single page using only one query in PHP

This snippet shows a MySQL query being used to update and insert data into a database: if ($_POST["ok"] == "OK") { $updateSQL = sprintf("UPDATE attend SET at_status=%s, at_remarks=%s WHERE at_tt_idx=%s", GetSQLValueString ...

Steps for setting up Angular 5 on your system

My current project requires Angular 5, but with the recent release of Angular 7, I'm unsure how to set up Angular 5 on my computer. I had transitioned from Angular 6 a few months ago. Do I need to uninstall the latest version to work with Angular 5? ...

Angular application experiencing issues with opening snackbar notifications

I'm currently working on a user registration application in Angular. My goal is to notify the user upon successful account creation or if an error occurs. I've been attempting to use a snackbar for this purpose, but it's not working as expec ...

Observing exceptional inquiries

Can I check what requests Protractor is waiting for? I'm working on fixing inconsistent state testing, but it's difficult to determine if a button didn't initiate a response or if Protractor simply didn't wait. In short: How do I see t ...

What could be causing the directives module to not get properly incorporated into the Angular app module?

I am currently in the process of learning Angular and have come across some challenges with module resolution. In js/directives/directives.js, I have a directive scoped within a directives module: angular.module("directives").directive("selectList", funct ...

Discovering the Cookie in Angular 2 after it's Been Created

My setup includes two Components and one Service: Components: 1: LoginComponent 2: HeaderComponent (Shared) Service: 1: authentication.service Within the LoginComponent, I utilize the authentication.service for authentication. Upon successful authent ...