Ways to update a property in a jQuery function

Is it possible to update the this.variable of the AuthDirective class using a jQuery function?

I may have a basic question, but I need to trigger Jquery events and manipulate Angular variables.

import { Component, Directive, OnInit } from '@angular/core';
import { AppGlobal                    } from '../../../app.global';
declare var $:any;

@Component({
  selector    : 'authorization',
  templateUrl : './auth.directive.html'
})
export class AuthDirective {
  isCaptcha : boolean = false;
  constructor(
    public  app : AppGlobal
  ) {
  }
  ngOnInit() {
    $('.sign-up-label').on('show.bs.modal', function (e) {
      this.isCaptcha = true; // ISSUE IN THIS CONTEXT, BUT WORKS IN THAT CONTEXT
    })
  }
}

{{isCaptcha}} always evaluates to false

<div class="form-group ta-web-right mt-3" *ngIf="isCaptcha">
  <re-captcha (resolved)="resolved($event)"></re-captcha>
</div>

Any assistance would be appreciated.

Answer №1

To retain the value of this from the outer context, you can save it in a variable like so:

ngOnInit() {
    var that = this;
    $('.sign-up-label').on('show.bs.modal', function (e) {
      that.isCaptcha = true;
    })

}

Answer №2

To avoid conflicts with the local context, store the outer context by using let self = this;.

Update your function as shown below:

ngOnChanges(changes: SimpleChange) {
    let self = this;
    $('.sign-up-label').on('show.bs.modal', function (e) {
      self.isCaptcha = true; // This change should be reflected in a different context
    })
  }

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

Why does JQuery AJAX require a nested DIV to function properly?

Consider the index.php file below : <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js"></script> </head> <body> <form id="MY_FORM" action="./index.php" method="get" ...

Creating a JSON response with embedded HTML in Django

I am facing an issue while attempting to send a JSON response in Django using Ajax. The JSON response is supposed to be a list, with each element containing some HTML code. However, when I try to retrieve and parse this response in JQuery after fetching it ...

contentScript encountering CORS problem while using $.ajax()

I encountered an issue with the $.ajax() function not working in my contentScript. The error message I received was: XMLHttpRequest cannot load http://example.com/tab/index.php. No 'Access-Control-Allow-Origin' header is present on the request ...

Generate a basic collection of strings from an object

Looking at this object structure Names = [ { group: 'BII', categories: null }, { group: 'GVL', categories: [] } ]; I ...

The inner workings of jQuery's each function and the array it contains

Struggling with the functionality of each function... Let me clarify with an example... Within my code, there is a DIV identified as "media-type-container-1", containing dynamic content: <div id="media-type-container-1"> <div><input ty ...

Typescript: Why Lines Are Not Rendering on Canvas When Using a For-Loop

What started out as a fun project to create a graphing utility quickly turned into a serious endeavor... My goal was simple - to create a line graph. Despite my efforts, attempting to use a for-loop in my TypeScript project resulted in no output. In the ...

When I try to load JSON data using the http.get() method in my Angular 2 template, it returns

I've encountered an issue while attempting to read and parse a local json file into a custom class I created. The problem arises when trying to access properties of the class, as it throws errors indicating that the class is either null or undefined. ...

Which media type should be designated for a page that includes a combination of PHP, HTML, CSS, and JavaScript/jQuery content, with a .php file extension?

When working with a PHP file that includes HTML, PHP, CSS, and JavaScript, what content type should be set in the header() function? header('Content-type: text/html; charset=utf-8'); I noticed that when I view the source code of the i-frame, th ...

Tips on how to automatically rearrange a jQuery UI sortable list

Currently, I am working with 2 jQuery UI sortable lists which can be found at http://jqueryui.com/demos/sortable/#connect-lists. The process involves dragging items from list A (catalog) to list B (basket). I have a specific requirement where I need the ...

Dealing with Javascript Array/Serialization in C# (.NET)

I have implemented tableDnD to rearrange table rows and then serialized them using the function "$.tableDnD.serialize()". Now, I need to pass this serialized data to C# for further processing. What is the most effective way to do this? Below is a sample ...

Retrieve the user information from Auth0 within the NestJS application

I am currently working on implementing Auth0 authorization in NestJS, but I am unsure of how to retrieve the user's data within the callback URL handler. In a normal express function, this issue could be resolved using the following code. The passpor ...

Ensure that the hover div remains open even after the mouse leaves, and only closes when clicked outside of the div window or on a

I have a jQuery script that controls the opening and closing of the "shopping_cart" div when hovering over the "shopping_button". $('#shopping_button, .shopping_cart').on('mouseenter',function(){ $(this).css('z-index',&apos ...

Utilizing Jquery for JSON Data Parsing

I have some JSON data that looks like this: {"product":["{productTitle=ABCD , productImage=/abcd.jpg, productPath=CDEF.html, productPrice=$299}","{productTitle=EFGH, productImage=xxxx.jpg, productPath=ggfft.html, productPrice=$299}"]} In my JSP page, I&a ...

Is there a way to update page content without having to refresh the entire page?

My goal is to refresh specific content on a page without having to reload the entire page using JavaScript or jQuery. Since my project is built in PHP and JavaScript, I encountered this issue. Note : I want the page content to refresh when a user performs ...

Struggle with incorporating a file

As part of the login process, I have two options available: easy login and standard login. The easy login requires an employee ID, birthdate, and captcha answer, while the standard login asks for first name, last name, birthdate, and captcha. To facilitate ...

Breaking down and modifying JavaScript JSON objects

Can someone explain how to separate a JSON object and make updates based on the ID? I've heard about using stringify! But how do I actually implement the function to update the object? <input type="text" value="{"id":"1","price":"30.00","edit":0}, ...

Tips for calculating the total count of a specific field within a JSON array with TypeScript

I have a JSON array. "user": { "value": [ { "customerNo": "1234" }, { "customerNo": "abcd" }, { "c ...

Can you explain the concept of a mapped type and its practical applications?

What is the function of this? And when would be the best scenario to utilize it? ...

Each loop in the forEach function triggers the mouseup event

I am currently utilizing a foreach loop: objects.forEach(function(object) { var button = '<tr><td>' + object.object.code + '</td><td>' + formatDistance(1456000) + &apos ...

Traversing a sequence of method calls within a Promise object (as the return type)

In software development, there is a classic technique where a method returns the result of another method call: method1(): ObjectX { if( condition1 ) return method2(); return undefined // or some default value; } method2(): ObjectX { let r ...