Using Closure Techniques in Typescript

I have a website built with Angular2 and Typescript. I am using the PrimeNG framework for CSS styling. Within my site, I have implemented a jQuery full calendar. My goal is to display a modal when an event on the calendar is clicked.

In the AfterViewInit interface method, I have initialized the fullCalendar object like this:

  $("#calendar").fullCalendar({
   //..some properties
   eventClick: function (calEvent) {
                this.display = true;
    }

  })

The "display" field is part of the component class. Setting it to true should show the modal, but it doesn't work as expected.

However, I also have a button on the same page with onClick handler:

public showDialog() {
        this.display = true;
    }

Clicking this button successfully shows the modal.

Here is the corresponding HTML code:

<div class="ui-g dashboard" style="margin-top: -18px">

  <div class="ui-g-12 ui-md-12">
    <div class="card">
      <div id="calendar"></div>
    </div>
  </div>
</div>
<p-dialog header="Godfather I" [(visible)]="display" modal="modal" width="300" responsive="true">
  <p>dadsasd</p>
  <p-footer>
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
      <button type="button" pButton icon="fa-close" (click)="display=false" label="No"></button>
      <button type="button" pButton icon="fa-check" (click)="display=false" label="Yes"></button>
    </div>
  </p-footer>
</p-dialog>
<button type="text" (click)="showDialog()" pButton icon="fa-external-link-square" label="Show"></button>

What could be causing the issue?

Answer №1

It is highly likely that Jquery assigns the element with id #calendar as this.

An alternative approach is to use an arrow function to maintain the same context.

$("#calendar").fullCalendar({
  //..some properties
  eventClick: (calEvent) => {
    this.display = true;
  }

})

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

Simple method to determine if a variable belongs to an enumeration

Among the numerous discussions on this topic, none seem to focus on developing a versatile function. This function should take an enum and a variable as input, check if the variable is part of that enum, and update the variable type if it is. I have made ...

The request to http://localhost:8000/api could not be completed due to a connection refusal error (

In the process of creating an Angular project aimed at displaying, adding, deleting, and updating employee details, I encountered persistent errors. Despite having my own API established with connections to Angular, Express, Node, and MongoDB, I continue f ...

What is the best method for transforming a Promise array (original File array) into an array of string values?

Basically, I have a function that transforms File type values into base64 string type values (as recommended by How to convert file to base64 in JavaScript?): export const customConvertFileToBase64String = (file: File) => new Promise<string | Array ...

Obtaining the attribute value from a custom tag in Angular: A comprehensive guide

I am currently working on creating a customized password component in Angular5. I am having difficulty obtaining the minimum and maximum attribute values required to validate the password. I attempted to retrieve the values using JavaScript's getAttr ...

What could be the reason for an empty FormData map being fetched when testing a fetch call in React?

Currently experimenting with a fetch call using Formdata in a react app while utilizing jest for testing. export const submitform = async (url:string, file:File, params:{}) => { const body = new FormData() body.append('file', file) if ( ...

Having trouble getting custom validation messages to function with jQuery

Within my form, I am dealing with the following input field: <input id="edit-submitted-tel" name="submitted[tel]" value="telefoonnummer (optioneel)" size="4" maxlength="15" class="form-text" type="text"> Although I have successfully implemented jQu ...

Tips for transferring a list via ajax to a controller

Having a controller as follows.. public ActionResult Report(List<string> invoiceIds) and utilizing an ajax call like this... function generateAccountsPayableReports() { var ms = $("#msInvoicesAPV").data("kendoMultiSelect"); var invoices = ...

What is the best way to prevent the double tap event on radio buttons for mobile devices?

My form with radio buttons works perfectly on the computer, but encounters issues on mobile devices. Users are required to double-tap to select a radio button, causing confusion. Is there a way to disable the double-tap requirement? I've looked at ot ...

Steps for generating an observable that mirrors the structure of an observable array

I am in the process of creating an observable array using JSON data retrieved from the server. var ViewModel = function (data) { var self = this; self.list = ko.observableArray(data); self.selected = ko.observable(); } ...

Incorporating a jQuery word count and limit within PHP code: a step-by-step guide

I am encountering an issue with a textarea count code. It functions perfectly on its own but when I integrate it as shown below, it stops working without throwing any errors. I have been trying to resolve this for more than 3 days now. Any assistance would ...

Creating a dynamic image slider that smoothly glides across a webpage

I've been working on a carousel (image slider) for my website and I need some help. Specifically, I want to reposition the entire slider slightly to the left on the webpage. You can see the slider in action on this site: and I also created a jsfiddle ...

Failed Ajax request to specified URL

After updating my Cordova version, I checked the current version using cordova -v and it showed 5.0.0. In the previous Cordova version (I can't remember which one), I was able to make AJAX requests to a URL successfully. However, after the update, ...

Create categories for static array to enable automatic suggestions

I have a JavaScript library that needs to export various constants for users who are working with vscode or TypeScript. The goal is to enable auto-complete functionality for specific constant options. So far, I've attempted to export a Constant in th ...

What is preventing the jQuery GET function from saving the dynamically sortable UI list?

I'm having trouble saving the order of my jquery ui sortable list to the server. When a user clicks a tab, I load the list from an external file and then add a checkbox to each item. The issue arises when I insert the line to prepend the checkbox - su ...

Ways to retrieve class variables within a callback in Typescript

Here is the code I'm currently working with: import {Page} from 'ionic-angular'; import {BLE} from 'ionic-native'; @Page({ templateUrl: 'build/pages/list/list.html' }) export class ListPage { devices: Array<{nam ...

Angular: Intercepting an HttpResponse to trigger additional calls before passing the response back to the caller

My current objective is to intercept a response and, if a certain value (CHALLENGE) is present, trigger a call to another API. I need to retry this process 3 times if necessary. Upon success, I should respond to the initial call and pass the result back to ...

Changing the click event using jQuery

My JavaScript snippet is displaying a widget on my page, but the links it generates are causing some issues. The links look like this: <a href="#" onclick="somefunction()">Link</a> When these links are clicked, the browser jumps to the top of ...

empty parameter in the POST request

For my upcoming project, I plan on utilizing jQuery Ajax for sending requests: var xhr = $.ajax({ url: "/UsersCounter", type: "POST", cache: false, data: {"emailAddr": "<a href="/cdn-cgi/l/ ...

The type 'number | undefined' cannot be assigned to the type 'number'

const initialProgram = { id: 0, studentId : 0, targets : [] } const [program, setProgram] = useState(initialProgram); In one of my functions I am updating the program as follows: setProgram({...program,studentId= selectedStudent?.id}) The t ...

Tips for swapping text with an image or icon during mobile scaling

As a newcomer to this field, I am facing challenges in finding specific answers. My current objective is to convert text labels into images when the viewport shrinks to mobile sizes. The complexity arises from the fact that I am employing Leaflet, a JavaSc ...