How can I integrate the jQuery Plugin Mapael with Angular 5?

While exploring various methods that tackled the issue of integrating jQuery Plugins, I decided to start with the fundamentals.

To begin with, I installed the jQuery plugin:

npm i jquery

Next, I included the TypeScript definition:

npm install -d @types/jquery

Incorporated it into my script array:

"apps": [{
  ...
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
  ],
  ...
}]

Afterward, I tested its functionality:

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

@Component({
  selector: 'app-ww-inventory-map',
  templateUrl: './ww-inventory-map.component.html',
  styleUrls: ['./ww-inventory-map.component.scss']
})
export class WwInventoryMapComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $(window).click(function () {
      alert('ok');
      });
  }


}

I received a response promptly...

https://i.sstatic.net/NZwei.png

Everything seemed to be working fine so far. With the first task completed, it was time to move forward. I then proceeded to install the jQuery plugin mapael:

npm i jquery-mapael

adjusted the angular-cli.json accordingly:

"apps": [{
  ...
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/jquery-mapael/js/jquery.mapael.min.js",
    "../node_modules/jquery-mousewheel/jquery.mousewheel.js"
  ],
  ...
}]

Subsequently, I attempted implementing this based on the code example provided on the official page

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

declare global {
  interface JQuery {
    mapael(map: any): JQuery;
  }
}

@Component({
  selector: 'app-ww-inventory-map',
  templateUrl: './ww-inventory-map.component.html',
  styleUrls: ['./ww-inventory-map.component.scss']
})
export class WwInventoryMapComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $(".container").mapael({
      map: {
        name: "world_countries"
      }
    });
  }
}

Initially, everything appeared to be in order without any warnings or issues displayed, but upon loading the page...

https://i.sstatic.net/z0kRn.png

At this point, I am uncertain about how to proceed correctly. I had hoped to resolve this matter somehow, yet none of the limited examples I found have proven to be particularly helpful. My apologies for posing what may seem like a repetitive question, but I am struggling to make it work.

EDIT 1: Initially, I attempted the solution provided by Naga Sai A, which required some alterations on my end to prevent any compiler errors. Thank you, Naga Sai A!

My .ts file appears as follows:

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import 'jquery-mapael';
import 'jquery-mapael/js/maps/world_countries.js';

declare global {
  interface JQuery {
    mapael(map: any): JQuery;
  }
}

@Component({
  selector: 'app-ww-inventory-map',
  templateUrl: './ww-inventory-map.component.html',
  styleUrls: ['./ww-inventory-map.component.scss']
})
export class WwInventoryMapComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $(".container").mapael({
      map: {
        name: "world_countries"
      }
    });
  }
}

https://i.sstatic.net/wr4K3.png

EDIT 2: Awaiting Peter's proposed solution

The imports are accurate, yet the component still requires further adjustments. Additionally, including Raphael.js is not mandatory. At least in my case, the map functioned seamlessly without it. I have maintained my initial declaration method.

Answer №1

Your script imports are causing the main issue. It seems that you are missing a required dependency for Raphael and you also need to import any maps you plan on using. Make sure to update your angular-cli.json with the following code snippet:

"scripts": [
  "../node_modules/jquery/dist/jquery.min.js",
  "../node_modules/raphael/raphael.min.js",
  "../node_modules/jquery-mousewheel/jquery.mousewheel.js",
  "../node_modules/jquery-mapael/js/jquery.mapael.min.js",
  "../node_modules/jquery-mapael/js/maps/world_countries.min.js"
],

Keep in mind the following notes from NPM:

When it comes to dependencies, jQuery and Raphael (and Mousewheel if necessary) should be loaded before Mapael for everything to function properly.

In terms of map files, they must be loaded after Mapael for them to work correctly.

https://www.npmjs.com/package/jquery-mapael#directly-in-your-page

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

//jquery declaration
declare var $: any;

@Component({
  selector: 'app-ww-inventory-map',
  templateUrl: './ww-inventory-map.component.html',
  styleUrls: ['./ww-inventory-map.component.scss']
})
export class WwInventoryMapComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $(".container").mapael({
      map: {
        name: "world_countries"
      }
    });
  }
}

Answer №2

If you want to get the desired outcome, make sure to follow the steps below to import jquery, jquery-mapael, and js for a map of world countries:

import * as $ from 'jquery';
import 'jquery-mapael';
import 'jquery-mapael/js/maps/world_countries.js';

Additionally, include the following code in your ngOnInit method:

ngOnInit() {
   $(".container").mapael({
      map: {
        name: "world_countries"
      }
    });
  }

You should see the results similar to the image displayed here:

https://example.com/my-screenshot.png

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

Incorporating an external TypeScript script into JavaScript

If I have a TypeScript file named test.ts containing the code below: private method(){ //some operations } How can I access the "method" function within a JavaScript file? ...

What is the best way to extract keys from a hash in Ruby using a Javascript string?

I am currently developing a command line tool using Ruby that is designed to parse JSON data from diverse sources and perform certain operations on the retrieved information. To make it user-friendly, I have incorporated a feature where users can configure ...

Guide on creating dynamic datasets and multiple dynamic yAxes

To enhance my chart, I am interested in adding multiple Y Axes based on the examples provided below: script 1 script 2 Although I attempted to modify the code as shown below, it seems to not be functioning properly. function rdnum() { return Math.flo ...

utilizing ajax for pagination in a codeigniter application

I am currently working on setting up pagination in my controller code: <?php $nama = $this->session->userdata('nama'); $start_row = $this->uri->segment(3); $per_page = 5; if (trim($start_row) == '') { $start_row = 0; }; ...

Facing an error response with the Javascript callout policy in Apigee. Any suggestions on fixing this issue?

This is the code snippet I'm using in my JavaScript callout policy var payload = JSON.parse(request.content); var headers = {'Content-Type' : 'application/json'}; var url = 'https://jsonplaceholder.typicode.com/posts'; va ...

Is it possible to create a Vue JSX component inside a Single File Component using the <script setup> syntax and then incorporate it into the template of the S

I am impressed by how easily you can create small components within the main component file in React. Is it possible to do something similar with Vue 3 composition API? For example: Component.vue <script setup> const SmallComponent = <div> ...

Is there a user-friendly interface in Typescript for basic dictionaries?

I'm not inquiring about the implementation of a dictionary in Typescript; rather, I'm curious: "Does Typescript provide predefined interfaces for common dictionary scenarios?" For instance: In my project, I require a dictionary with elements of ...

Discover the subsite inventory of a SharePoint site using TypeScript

Is there a way to gather all sub-sites from my SharePoint site and organize them into a list? I initially thought of using this.context.pageContext, but could not locate it. Please excuse my seemingly simple question, as I am still learning TypeScript. ...

Angular.js has been activated with the chosen:open event

I've been implementing the chosen directive for AngularJS from this source and so far it's performing admirably. However, my goal is to trigger the chosen:open event in order to programmatically open the dropdown menu as outlined in the chosen do ...

Store a video file on your device's memory

Currently, I am storing simple strings in local storage. However, I am now facing the issue of wanting to save videos and images to local storage. Due to the limitations of localStorage supporting only strings, I am unsure of how to achieve this. If you h ...

Need for utilizing a decorator when implementing an interface

I am interested in implementing a rule that mandates certain members of a typescript interface to have decorators in their implementation. Below is an example of the interface I have: export interface InjectComponentDef<TComponent> { // TODO: How ...

An informative step-by-step approach to constructing Angular applications utilizing npm and TypeScript

When I first encountered Angular2, I was introduced to TypeScript, npm, and more for the very first time. I was amazed by their power, but I know I've only scratched the surface. While I can navigate through the "development mode," my ultimate goal i ...

Setting an AJAX response as the value of a modal form

One of the functionalities on my website involves a button that, when clicked, triggers the opening of a modal form and sends an ajax request to a php page. This request includes an id from a specific table in the database. The php page then retrieves a ...

Tips on how to remove the preset text from a textbox with Python Selenium

Has anyone encountered difficulty clearing default data from a textbox using Python Selenium? I keep getting an 'element not interactable' error. Here is the HTML code: <div class="emailAttachmentInputMobile"> <input type=&quo ...

React application created with create-react-app that uses multiple environment files for varying configurations

I've been working on setting up various environment files for a React project (using create-react-app). I referred to the official documentation, but I encountered the following error: '.env.development' is not recognized as an internal or ...

Issues with connecting to Socket.IO in Cordova app

I'm having troubles setting up my Cordova app to establish a socket.io websocket connection. Despite following the instructions I found, it doesn't seem to connect when running in debug mode. Can anyone help me troubleshoot this issue? Server Si ...

How to Populate Object Literal with Multiple Objects in AngularJS

When I click on "Evan", I receive the following response: {"id":1,"name":"Evan"} If I click on "Robert", I will get: {"id":2,"name":"Robert"} Is there a way to modify this code so that it follows the aforementioned steps and generates an object similar ...

step-by-step guide on implementing autocomplete using google.maps.places.autocomplete in ExtJS

Just to clarify... I initially wrote a code that was error-free before editing this post. However, after reading a comment on creating a Minimal, Complete, and Verifiable example, I became confused. My minimal script did not work properly, I couldn't ...

6 Ionic date-time selector

I seem to be encountering some challenges while using Ionic 6 with the new date-time picker. The issue arises when I retrieve a value from the database through a nest service. In the database, the date appears as: “2022-06-30 13:11:54” but upon retriev ...

Using Jquery to set up an event listener on a child div that changes the CSS property of its parent div

I am currently working on a product customization form where the radio buttons are hidden using opacity:0. My goal is to have the border of the image turn black when an option is selected as a visual feedback for the user. I've implemented an event l ...