Guide on incorporating jQuery-extended bootstrap components in Angular

Currently, I am exploring the integration of the Bootstrap 3.3.7 popover component (since I cannot use the ng version) into a new Angular 4 application. To achieve this, I initially installed:

npm install --save jquery @types/jquery bootstrap

Following that, I included the necessary CSS and scripts in angular-cli.json file:

 "styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.css",
    "../node_modules/bootstrap-tour/build/css/bootstrap-tour.css",
    "styles.css"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/bootstrap/dist/js/bootstrap.js",
    "../node_modules/bootstrap-tour/build/js/bootstrap-tour.js"
  ]

Then, within the typings.d.ts file, I defined an interface as follows:

interface Jquery { 
   popover(...any) : any;
}

Afterwards, I imported jQuery into my component using the code snippet below:

import * as $ from "jquery"

However, upon attempting to execute the popover method, I encountered a compilation error:

('#test1').popover('show'); // An error is triggered here for the popover method.

How can TypeScript be configured to recognize it properly?

Answer №1

Here is the method I used to make this work on a brand new Angular project generated with the cli:

npm install --save jquery <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41232e2e35323533203101726f726f">[email protected]</a>

I made changes to the app.component.ts file as follows:

import { AfterViewInit, Component, ElementRef, ViewChild } from '@angular/core';

declare const $: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  @ViewChild('div') divElement: ElementRef;

  ngAfterViewInit(): void {
    $(this.divElement.nativeElement).popover({content: 'foobar', placement: 'bottom'}).popover('show');
  }
}

and updated the app.component.html accordingly:

<div #div>hello there</div>

By adding the scripts and styles to the angular-cli.json, these files are included in the global scope. Importing them as you normally would could lead to duplicate imports of jQuery, particularly after the bootstrap scripts.

While it's generally preferred to import scripts in a way that allows typescript/webpack to handle them efficiently, in this case, using the global scope method seems necessary due to bootstrap's dependencies on jQuery. To avoid errors in typescript though, include the line declare const $: any;

You can find the complete GitHub repository here

I hope this explanation proves helpful!

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

I want to make my code available as an npm library on GitHub, but when users install it, I want them to only get

When pushing the code to GitHub, I also need to include the dist folder. However, when running the command npm i <github-url>, I want the node_modules files to be downloaded and placed inside the dist folder. ...

Creating a new project with Angular CLI version 7.2.1 is encountering some issues

Recently, I decided to upgrade my Angular CLI to version 7.2.1. However, after completing the upgrade, I encountered an issue when trying to create a new project using the command: ng new hello-ng-world The command failed and displayed the following erro ...

Issue: Module not found; Typescript error encountered in a react application

I have a load_routes.js file in the node_express folder that is responsible for loading all the routes for my project. Everything was working smoothly until I decided to change the file extension from .js to .ts. Suddenly, I started encountering the follow ...

Changing the color of a specific span using Angular

I am working with a dynamic mat-table where columns are added and populated on the fly. The table headers are styled using divs and spans. My goal is to change the color of a header to black when clicked, but also un-toggle any previously selected header. ...

What is the best way to define a model class within my Angular 2 component using TypeScript?

As I delve into Angular 2 and TypeScript, I am keen on adopting best practices. I have decided to move away from a simple JavaScript model ({ }) in favor of creating a TypeScript class. However, it seems that Angular 2 is not very fond of my approach. T ...

What are the steps for setting up Angular and Rails on Nginx for deployment

I have a challenge with setting up my NGINX server. I am hosting both the Angular and Rails servers on the same instance and need guidance on how to make them work harmoniously on the same server. Here's what I've done so far: NGINX configurat ...

Compilation error in VueJS: missing dependency detected

I am facing an issue in my VueJS project where a file I am referencing seems to be causing a compilation error. Despite being present in the node_modules directory, the dependency is declared as not found. In the image on the left, you can see the directo ...

Ways to efficiently update the API_BASE_URL in a TypeScript Angular client generated by NSwag

Is it possible to dynamically change the API_BASE_URL set in my TypeScript client generated by NSWAG? I want to be able to utilize the same client with different API_BASE_URLs in separate Angular modules. Is this achievable? Thank you for your assistance. ...

I'm curious why my phone number is being treated as an object when it's passed in as a prop

I am facing an issue with my FooterScroll component while using it on the TvIndex main page. This is the FooterScroll Component const FooterScroll = (Id: number) => { const { event } = useEvent(Id); console.log('Id', Id); return ( ...

The Ionic tab is already finished displaying even before the data has completed loading

Creating a favorites section for a vouchers app has proven to be slightly challenging. When attempting to retrieve the current user's favorite vouchers and display them using ngFor in the HTML, I encountered an issue. The very first time I open the fa ...

Is it advisable to include auto-generated files in an npm package upon publication?

I have a TypeScript NPM package where my build process compiles all *.ts files into myLib.d.ts, myLib.js, and myLib.js.map. In order for my NPM package to function properly, it requires the src/*.ts files as well as the auto-generated myLib.* files. Howe ...

Angular 2 Error: TS2322 - The type 'Subscription' cannot be assigned to the type 'Observable<MouseEvent>'

I have implemented the click-outside directive using this plunk --> http://embed.plnkr.co/v7BMUv/ But when I try to compile my TypeScript code, I encounter the following errors: Error TS2322: Type 'Subscription' is not compatible with type & ...

(angular 8) Error occurred when converting a file or image to FormData due to an invalid value (Note: FormData object printed as Object Form{})

I encountered an issue where I am getting an invalid value from form data. The value appears correct in `this.fileData` with a size of 5701, but becomes empty when converted to form data - `{}` is logged when I console.log the form data. Additionally, acce ...

leveraging Angular 2 in combination with an express-node js API

Hello, I’m currently trying to wrap my head around the installation of Angular JS v2. After going through numerous tutorials, I find myself feeling quite confused. Some tutorials mention using webpack to set up a server for the application, while other ...

Dynamic starting point iteration in javascript

I'm currently working on a logic that involves looping and logging custom starting point indexes based on specific conditions. For instance, if the current index is not 0, the count will increment. Here is a sample array data: const data = [ { ...

Update the content of a page on an Ionic application every few minutes

I am currently developing an application that requires the data displayed on the page to be refreshed automatically every 15 minutes, even if there is no user interaction happening at that time. What would be the most efficient method to accomplish this t ...

Error encountered in Snap SVG combined with Typescript and Webpack: "Encountered the error 'Cannot read property 'on' of undefined'"

I am currently working on an Angular 4 app that utilizes Snap SVG, but I keep encountering the frustrating webpack issue "Cannot read property 'on' of undefined". One solution I found is to use snapsvg-cjs, however, this means losing out on the ...

Guide on creating a TypeScript function that extracts specific properties from an object using a filter criteria

Look at the code snippet provided below. interface IEmployee { ID: string Employee: string Phone: number Town: string Email: string Name: string } // Retrieve all properties of type IEmployee let allProps = findSpecificItem<IEm ...

The process of invoking the AsyncThunk method within the Reducer method within the same Slice

Within my slice using reduxjs/toolkit, the state contains a ServiceRequest object as well as a ServiceRequest array. My objective is to dispatch a call to a reducer upon loading a component. This reducer will check if the ServiceRequest already exists in ...

Encountering an issue while attempting to initiate a nested array: "Cannot assign a value to an optional property access in the left-hand side of an assignment expression."

I am dealing with an object that contains nested arrays, structured like this: export class OrdenCompra { public id?: number, public insumos?: OrdenCompraInsumo[], } export class OrdenCompraInsumo { id?: number; traslados?: IImpuestoTraslado[]; } export ...