Automate the process of opening an ngbpopover from an Angular 2 component using programming techniques

Currently, I am referring to this specific article in order to integrate Bootstrap with Angular 2. While the instructions in the article are helpful, there seems to be a lack of information on how to pass the popover reference to a component method. The only thing mentioned is an NgbPopoverConfig, which does not have the ability to open the popover.

Here is an example of my component method:

popOpen(p : NgbPopoverConfig): void {

    p.open(); //An error occurs here stating no Open method on NgbPopoverConfig
 }

Below is the snippet of HTML where a Material 2 based input field is used:

<md-input type="email"  value=""  name="em" id="em" placement="left" ngbPopover="adfga"  popoverTitle="Popover on left" triggers="manual" #p="ngbPopover" (click)="p.Open()"  [(ngModel)]="thisModel.email" 
                          #this="ngModel">
</md-input>

Is there any documentation available for programmatically opening and closing the popover from the TypeScript component code? Or perhaps some sample code that can be shared?

My current setup includes Angular 2.1.0, Bootstrap 4.0.0-alpha.5, and Angular Material 2.0.0-alpha.9-3.

Answer №1

Looks like there was a typo: it needs to be open instead of Open. Remember, Javascript is case sensitive.

Code Example Link

Answer №2

Resolved the issue by following two key steps. Firstly, I obtained the reference to ngbPopover

import {NgbPopover } from '@ng-bootstrap/ng-bootstrap/popover/popover';

and modified my component method as follows:

popOpen(p : NgbPopover): void {

    p.open();  
 }

then added the event handler like this:

<md-input type="email"  value=""  name="em" id="em" placement="left" ngbPopover="adfga"  popoverTitle="Popover on left" triggers="manual" #p="ngbPopover" (blur)="edit=!edit; checkExists(p)"  [(ngModel)]="thisModel.email" 
                          #this="ngModel">
</md-input>

Now, upon losing focus on the textbox, a component method is triggered to verify the email existence. If found, the p reference is used to execute the open() method.

I'm curious about this line: edit=!edit; Can someone explain how it enables the registration of the blur event?

Answer №3

Below is the code snippet that successfully executed for me

import {Component} from '@angular/core';
import {NgbPopover } from '@ng-bootstrap/ng-bootstrap/popover/popover';
@Component({
  selector: 'ngbd-popover-triggers',
  templateUrl: './popover-triggers.html'
})
export class NgbdPopoverTriggers {
condition : boolean = true;
  openPop(popover : NgbPopover): void {
    console.log(this.condition);
    popover.isOpen() ? popover.close() : popover.open();
  }
}

Here's the HTML markup for the component

<br>
<br>
<br>
<br> 

<div>
<ng-template #popContent>Hello, <b>{{name}}</b>!
<button (click)="onClickYes()" type="button" class="btn btn-outline-secondary" >
  Yes
</button>
<button (click)="onClickNo()" type="button" class="btn btn-outline-secondary" >
  No
</button>
</ng-template>
<ng-template #popTitle>Fancy <b>content!!</b></ng-template>
</div>

<button placement="top" [popoverTitle]="popTitle" #popover="ngbPopover"  [ngbPopover]="popContent"  triggers="manual" (click)="openPop(popover)">click me </button>

Answer №4

If you want to make use of open or close API in a lower environment, there are certain steps that must be followed:

Make sure you have ng-bootstrap version 1.x.x, Angular version 5.0.2, and Bootstrap CSS version 4.0.0.

To start with, in your component.ts file, import 'NgbPopover':

import {NgbPopover } from '@ng-bootstrap/ng-bootstrap';

  openPop(popover : NgbPopover): void {
    this.popover.isOpen() ? this.popover.close() : this.popover.open();
  }

Next, in your HTML template, add the following code:

<button #popover="ngbPopover" placement="bottom" ngbPopover="content goes here"  popoverTitle="Popover Title" triggers="manual" (click)="openPop(popover)"></button>

It's important to note that if your setup differs, make sure to refer to their official documentation for any modifications required.

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

What sets TypeScript apart from AtScript?

From what I understand, TypeScript was created by Microsoft and is used to dynamically generate JavaScript. I'm curious about the distinctions between TypeScript and AtScript. Which one would be more beneficial for a JavaScript developer to learn? ...

Hover over to reveal the button after a delay

I'm struggling with implementing a feature in my Angular code that displays a delete button when hovering over a time segment for 2 seconds. Despite trying different approaches, I can't seem to make it work. .delete-button { display: none; ...

Retrieve the predetermined value from the dropdown menu option

I currently have two classes with a mapping structure as follows: User *--------------------1 Sexe Users are listed in the file list-users.component.html. When selecting a user for modification, I am redirected to the modify-user.component.html B ...

The command "npm start" is currently experiencing issues, showing an error message stating that it failed at the start script for the project

I have been using this link as an example to learn Angular. However, when I try to run npm start, it shows an error. I have looked for solutions and they suggest updating either npm or Angular versions, but I am already using the latest versions: npm -v = ...

Exploring the mocking of document,hidden using Jasmine

Struggling to simulate document.hidden in an angular unit test, but facing issues. Tried the following solutions: spyOn(Document.prototype, <any>'hidden').and.returnValue(true); spyOn(Document, <any>'hidden').and.ret ...

Using Visual Studio Code Build Tasks in Harmony

The documentation for Visual Studio Code includes examples of tasks.json configurations that allow for either typescript compilation or markdown compilation, but does not provide clear instructions on how to achieve both simultaneously. Is there a way to ...

Discovering the versatility of Typescript objects

I want to define a type that follows this rule: If the property container is present, then expect the property a. If the property item is present, then expect the property b. Both container and item cannot exist at the same time. The code I would expect ...

"Unexpected compatibility issues arise when using TypeScript with React, causing errors in props functionality

Just the other day, my TypeScript+React project was running smoothly. But now, without making any changes to the configurations, everything seems to be broken. Even rolling back to previous versions using Git or reinstalling packages with NPM does not solv ...

Can the validity of a Control's property be adjusted?

Is it possible to establish the valid property of a control titled "titleCtrl"? I attempted .dart titleCtrl.valid = false; Unfortunately, this results in an error. However, retrieving the valid state poses no issue. ...

Angular2 - Showing parameters in modal interface

I am working on an Angular5 app and have a component.html file with a function called markerClick that opens a modal. However, I am facing challenges in displaying the item.lat parameter in the modal and would appreciate your assistance. Below is the code ...

Ensure to update outdated dependencies before installing them in an Angular project

Would it pose a risk to utilize a library with an outdated version of Angular compared to the project's current version? Is it advisable to employ npm i --force in such scenarios? ...

Encountering issues with executing transactions in an Ionic 2 application when utilizing Sqlite functionality from Ionic Native

I am currently working on a project that involves setting up and querying a local sqlite database. I am utilizing the cordova-sqlite-plugin along with Sqlite from Ionic Native. Here is the code snippet responsible for opening the database and creating tab ...

Learn the process of adjusting the Time Zone in Angular2-HighCharts!

I've been struggling for a few days now trying to adjust the UTC time in an area chart using Angular2-HighCharts. The backend API is returning timestamps which I then inject into the chart, but each time it's being converted to "human time" with ...

Ways to show a div when a dropdown option is selected

Currently, I am in the process of designing a form that includes a dropdown menu. Once the user selects a particular option from the list, such as: Software developer a new section will be displayed, showing two additional options: App Developer ...

Creating layers of object declarations

Looking for assistance on the code snippet below. type Item = { id: number; size: number; } type Example = { name: string; items: [ Item ]; } var obj: Example = { name: "test", items: [ { i ...

Tips for inserting an object into an array

Here's the data I received: { 0:{modifierId: 4, modifierName: 'Garlic', modifierPrice: 60 } 1:{modifierId: 1, modifierName: 'Tartar ', modifierPrice: 60} 2:{modifierId: 3, modifierName: 'Herb ', modifierPrice: 60} item ...

Transforming an Observable to a boolean using RXJS

Hey there, I'm currently working on creating a function similar to this: verify(){ return this.http.post(environment.api+'recaptcha/verify',{ token : this.token }).pipe(map(res=>res.json())); } I want to be able to use ...

Use a mock HTTP response instead of making an actual server call in Angular 4

I am facing a scenario where I have a function myFunc() that I subscribe to. When this function is called with X parameter, I expect a regular HTTP response from the server. However, if it is called without the X parameter, I want it to return a 'mo ...

How do I add a new item to an object using Ionic 2?

example item: this.advData = { 'title': this.addAdvS2.value.title , 'breadcrumb': this.suggestData.breadcrumb, 'price': this.addAdvS2.value.price ...

Dealing with precompile array warning when utilizing a basic router in Angular 2

I am currently working on developing a straightforward router-based application in Angular2 using typescript. The version of Angular2 I am using is 2.0.0-rc.4, and the router version is 3.0.0-beta.1 Here is my Routes configuration- App.routes.ts import ...