Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.modal() function not working in angular 8

On Click, "$("#Popup").modal('show')" is not working.

HTML

<a class="btn-profile-login" data-target="#Popup" (click)="loginBtn()">{{SignText}}</a>

TS

import { Component, OnInit, ViewChild } from '@angular/core';
import * as $ from 'jquery';
import * as bootstrap from "bootstrap"; 

export class SampleComponent implements OnInit{
    loginBtn() {
        $("#Popup").modal('show');
    }
}

Error Message:

TypeError: jquery__WEBPACK_IMPORTED_MODULE_9__(...).modal is not a function

like image 697
3103 Avatar asked Jun 14 '26 23:06

3103


2 Answers

You can follow these step

npm install jquery --save
npm install @types/jquery --save

Then in scripts section in architect => build of angular.json file we add path for jquery lib

"scripts": [
  "node_modules/jquery/dist/jquery.min.js"
]

Then modify tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "types": ["jquery"] // addd jquery here
  },
  "exclude": ["test.ts", "**/*.spec.ts"]
}

Then you can remove this import since you already have type definition of jquery

import * as $ from 'jquery';
like image 133
Tony Ngo Avatar answered Jun 16 '26 12:06

Tony Ngo


Use ngx-bootstrap https://valor-software.com/ngx-bootstrap/ It is Angular Bootstrap components that do not require jQuery. It is the official way to use Bootstrap with Angular, you should avoid to include jquery in angular apps when possible

All the best.

like image 22
Adrian Brand Avatar answered Jun 16 '26 13:06

Adrian Brand