Have you heard about using Angular-cli to bootstrap with version 3 along with jQuery integration in Angular

As I prepare to embark on a new project, I am considering Angular 2.0 as a solid framework to utilize for its routing, components, and directives. However, there are existing elements built with Bootstrap v3 and jQuery that I would like to incorporate into my project. Is it possible to integrate Bootstrap v3 and jQuery within the structure created by Angular-cli?

I have noticed other Angular 2 seeds using Gulp, but they all seem to recommend the official Angular-cli instead.

Additionally, I am unfamiliar with how to introduce an existing JavaScript library into Typescript and whether or not jQuery's global $ will cause conflicts with Typescript.

Please note: While I am aware of ng2-bootstrap, I have concerns about compatibility since it does not rely on jQuery as a dependency. Also, I understand that using jQuery in Angular is not ideal practice, but there are instances where it may be necessary for plugins or dependencies.

Answer №1

If you're looking for a temporary solution while waiting for better support of 3rd party libraries, I have a workaround that worked for me :)

First, run the following commands in your terminal:

npm install jquery 

and

typings install jquery --ambient --save

In your angular-cli-build.json file, make sure it looks like this:

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      ...
      'jquery/**/*.js'
    ]
  });
};

Then, in your system-config.ts file, add the following mapping:

/** Map relative paths to URLs. */
 const map: any = {
   'jquery': 'vendor/jquery'
 };

Next, include the jQuery script in your src/index.html file:

<script src="/vendor/jquery/dist/jquery.min.js" type="text/javascript"></script>

Now, in the component where you want to use jQuery, declare it as follows:

declare var $:any;

@Component({
})
export class YourComponent {
  ngOnInit() {
    $.("button").click(function(){
       // Do whatever you need to do here
     });
     console.log();
  }
}

This approach has been successful for my project. Hopefully, it will also be beneficial for you :) .

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 is the best method for incorporating dynamic page transitions when navigating between individual pages?

Let's say I have the following pages: red.html blue.html green.html My goal is to create links from red.html to blue.html and red.html to green.html. When attempting to use page anchors and iframes, I encountered an issue where scrolling from red.h ...

Develop a loading modal for all AJAX requests, with the exception of a specific one

I've implemented a code snippet to display a modal for ajax requests: $(document).ajaxStart(function () { showLoadingModal(); }); $(document).ajaxError(function () { closeLoadingModal(); alert("error"); }); Although this code creates a modal ...

Leveraging both chained 'done' and 'then' for numerous asynchronous operations

Within my code, I have two functions containing ajax calls - setEmployees and getAllRecordsForEdit. I require certain code to execute after setEmployees completes, another set of code to run after both setEmployees and getAllRecordsForEdit finish, and addi ...

Looking for a button that can be toggled on and off depending on the input fields

Even after adding useEffect to my code, the button component remains disabled unless the input fields are filled. It never enables even after that. export default function Page() { const [newPassword, setNewPassword] = useState(''); const [conf ...

'Mastering the implementation of promises in React context using TypeScript'

I've been diving into the world of incorporating TypeScript in React and I'm facing a challenge with implementing async functions on context. The error that's popping up reads as follows: Argument of type '{ userData: null; favoriteCoc ...

Encountering a NULL argument in an MVC controller when executing an Ajax post request

I am facing an issue with my ajax post request where I send JSON string data, but it is showing null in my controller. Here is the AJAX Call: var jsonData = JSON.stringify(matrixPresenter); $.post("/matrix/Savematrix?matrixData=" + jsonData , fu ...

"Can you guide me on how to pick out one item before making my

Can someone please assist me with setting a default value for my input:hidden element before clicking on it? Here is the code I have: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> ...

Generate a new div element by simply tapping on a button

I want to create a new div by clicking on a button that contains a form. Each div (form) should include information about a specific plane. Once I click on the save button, each plane should be saved in the database. I have started creating the form but I ...

As the background image shifts, it gradually grows in size

I'm attempting to create an interesting visual effect where a background image moves horizontally and loops seamlessly, creating the illusion of an infinite loop of images. Using only HTML and CSS, I've run into an issue where the background ima ...

What is the best way to update data upon clicking a link using ajax?

My current code implementation is not functioning as expected. The goal is to display data in a div based on the link that is clicked - clicking on link 1 should show link 1's data, and clicking on link 2 should replace it with link 2's data. How ...

Struggling to convert a vb.net string into a json string through Serialization

I'm struggling with the following code, can someone please point out where I'm making a mistake? VB For Each dr As DataRow In dvItems.Table.Rows strItems &= "'" & dr("ItemTitle") & "'," Next strItems = strItems.Trim("," ...

Wordpress AJAX delivering empty response instead of intended string

I have tried everything, including adding die() at the end of my PHP function, but my ajax call is still returning zero. I went through other questions here but could not find a solution, so please review my code. This is the function I use to make an aja ...

Is JavaScript necessary for this task in PHP?

Hi everyone, I recently discovered a way to style a PHP echo in a more visually appealing manner. Instead of presenting an unattractive colored box right away, I was wondering if there is a way to only display the box when the user selects the calculation ...

"Encountering a 500 error on Chrome and Internet Explorer while trying to sign

I am currently working on an ASP.NET Core application that handles identity management through Azure AD B2C using the ASP.Net Core OpenID Connect. The front end is developed using AngularJS 2 with TypeScript. In my Logout function, the user is redirected t ...

What is the best way to store information in JSON format and transmit it to a server using an API?

I'm looking to save the form data in JSON format and send it from my UI to the server. I've searched through numerous sources but haven't found a solution yet. Struggling with the basic design structure, any help would be greatly appreciat ...

What is the best way to handle multiple requests to the same URL in Cypress while waiting?

I am currently developing a Cypress test that involves sending multiple requests to the same URL with varying body content. Essentially, the test modifies input values in the user interface, triggering new server requests. The challenge arises when trying ...

Issue with CakePHP 3: The $this->set method is not returning any response data following a POST

I'm facing an issue where I am unable to retrieve a response from my controller to my view files. Even though I have confirmed that the controller contains the correct data by using echo for verification, it results in an error related to header emiss ...

Preventing the addition of hash tags to the URL by the anything slider

Why does the Anything Slider add hash tags like #&panel1-1 to the end of the URL? I attempted using hashtags:false but it was unsuccessful. Are there alternative methods to prevent it from creating these hashtags? ...

A guide to implementing Bootstrap 4's toolchain for customized downstream applications

After reading the themed page of the Bootstrap documentation, I've managed to compile a custom.scss file into CSS in my NPM project following the recommended file structure: my-project/ ├── scss │ └── custom.scss └── node_module ...

Angular 7's URL updates, but no other actions take place

I am new to posting here and feeling quite desperate. Currently, I am working with Angular 7 and facing an issue. The problem arises when I manually enter the desired URL, everything works perfectly. However, when I use RouterLink by clicking a button, the ...