Tips for assigning a value to a reactive form control in Angular by utilizing the value retrieved from a callback function

Currently utilizing bootbox, I am able to retrieve the value from a prompt and display it using a callback function. However, I am facing an issue where I cannot set the same value in one of the form controls ('overallPercent') of 'addAssessmentForm'.

(I aim to assign the value obtained from the prompt input box on button click to a reactive form input box)https://i.sstatic.net/Mit79.pnghttps://i.sstatic.net/v2dyv.png https://i.sstatic.net/MDAWI.png

HTML Code :

<div>
  <input type="checkbox" class="form-check-input" [attr.disabled]="disabledLocation" formControlName="rural" (click)="onRuralClick($event)" />
  <label class="mx-2"> Rural</label>
</div>

TS Code :

onRuralClick(e) {
  let ans;
  let ruralCheck = e.target.checked;
  if (ruralCheck == true) {
    bootbox.prompt({
      title: "Enter Ratio",
      size: 'small',
      centerVertical: true,
      buttons: {
        cancel: {
          label: '<i class="fa fa-times"></i>',
          className: 'btn-danger',
        },
        confirm: {
          label: '<i class="fa fa-check"></i>',
          className: 'btn-success'
        }

      },
      callback: function(result: number) {
        ans = result;
        console.log(ans);
      },



    });

    // this.addAssessmentForm.controls['overallPercent'].setValue(ans);
  }


}

A method that initializes all form controls:

createForm() {
  this.addAssessmentForm = this.fb.group({
    assessmentId: [],
    assessmentName: [
      ],
    ],
    assessmentDescription: [null, [Validators.maxLength(200)]],
    startDateCheck: [],
    startDate: [],
    endDateCheck: [],
    endDate: [],
    totalNumberOfDay: [this.calnumofDays, [Validators.required]],
    class: [],
    status: [true],
    rural: [],
    urban: [],
    overallPercent: [],
  });
}

Answer №1

To access class variables, simply utilize the arrow function:

onRuralClick(e) {
  let ans;
  let ruralCheck = e.target.checked;
  if (ruralCheck == true) {
    bootbox.prompt({
      title: "Enter Ratio",
      size: 'small',
      centerVertical: true,
      buttons: {
        cancel: {
          label: '<i class="fa fa-times"></i>',
          className: 'btn-danger',
        },
        confirm: {
          label: '<i class="fa fa-check"></i>',
          className: 'btn-success'
        }

      },
      callback: (result: number) => {
        ans = result;
        this.addAssessmentForm.controls['overallPercent'].setValue(ans);
        console.log(ans);
      },



    });

    
  }


}

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 could be causing the module to break when my Angular service, which includes the httpClient, is added in the constructor?

After creating a backend RESTful API, I encountered difficulties while trying to access it. To address this issue, I developed a database-connection.service specifically for making POST requests. However, I am facing challenges in implementing this solut ...

"Exploring the relationship between Vue checkbox components: parent and

Currently, I am working on a checkbox group where the behavior is such that if the parent checkbox is checked, all the child checkboxes will also be checked. Similarly, if a child checkbox is checked, the parent checkbox should also get checked, and so on. ...

What is the best way to obtain multiple hexadecimal values from a URL query string using jQuery?

Here's the image tag I'm working with: <img src="http://www.example.com/render/pattern?obj=patterns/pattern_1&color=7F8C6C&obj=patterns/pattern_2&color=C8D9B0&obj=patterns/pattern_3&color=FFFFD1" width="100" height="100" a ...

Is there a way to ensure my jquery post request functions properly with the variable retrieved from a function upon clicking?

I've hit a roadblock trying to pass a game's name to my php function when a user clicks a button. The concept is simple: the user clicks a button with the video game's name as its value, then the php script checks the ranking of that game an ...

Is there a way to simplify this code using jQuery functions?

I am in the process of converting my Jquery code into a function to enhance its flexibility. The function will accept parameters such as the ID of the Handlebars SCRIPT tag, the ID of the DIV tag where the output will be displayed, the AJAX query URL, and ...

Can someone assist me in creating a clickable link that opens a menu in HTML when clicked?

I have been attempting for the past few days to open the megamenu by clicking on a link, but despite my efforts, I have not been successful. After reviewing some code, I discovered a clue in the CSS. It seems that setting the visibility value to visible wi ...

An easy fix for handling various model and view values in Reactive input controls within Angular applications

Within my formControl, I have set the value to an object like {id: 1, name: 'Ramesh'}. The input field inside a mat-form-field is read-only and the value is either retrieved from a database or selected from a pick list. In order to display a dif ...

An issue with the THREE.js TextureLoader not functioning properly when deployed

After implementing the code below, my project is successfully rendering my background and avatar. However, when deploying on Netlify, the rendering does not function as expected. I have attempted using "../../", "./", and other variations, but the issue p ...

Tips for highlighting HTML syntax within JavaScript strings in Sublime Text

Is there a Sublime package available for syntax highlighting HTML within JavaScript strings specifically? (Please note that the inquiry pertains to highlighting HTML within JS strings only, not general syntax highlighting.) Currently, I am developing Ang ...

Is there a restriction on the amount of data that can be

I have provided the code snippet below for reference: <System.Web.Services.WebMethod()> _ Public Shared Function LinkedUSNs(ByVal usn As String, ByVal requester As String, ByVal reason As String, ByVal terminalip As String, ByVal strCon As Strin ...

Incorrect indentation when using the latest Prettier in conjunction with Angular 17 and VS Code

Utilizing the most recent version of prettier with the following configuration file: { "arrowParens": "always", "bracketSameLine": true, "bracketSpacing": true, "semi": true, "experiment ...

Implementing ng-bootstrap popovers using dynamic data from a database

I am looking to incorporate nb-bootstrap popovers for specific text segments retrieved from a database. While I have successfully integrated ng-popovers in other parts of the code base within the .ts files, attempting to do so with content sourced from th ...

Address Book on Rails

Hello, I'm relatively new to this and would be grateful for any assistance. My goal is to utilize the data saved by a user in their address book, and then offer them the option to use that address for delivery. Below is my Address controller: class A ...

Is there a way to obtain HTML code within a contentEditable DIV?

When working in a contentEditable-DIV, my goal is to extract the HTML code from the starting position (0) to the end position where the user has clicked. <div id="MyEditableId" contentEditable="true"> 1. Some text 123. <span style="background-c ...

Chrome browser is not reachable due to a web driver error when running tests with the Protractor

Dealing with issues in Protractor end-to-end testing environments can be quite common. Despite searching extensively, I have yet to find a satisfactory solution for my specific problem. I am currently using the Protractor framework with the Jasmine test ru ...

A child support route isn't being displayed

I struggle to grasp the precise algorithm utilized by the angular router, especially when it comes to auxiliary routes. My Attempts Despite scouring through documentation, I couldn't find clear insights into how route matching works, particularly for ...

Sending a JSON object as a string to PHP

I am currently using a JQuery script to manipulate an image. While most functions are working correctly, I am facing issues with returning data to PHP. Despite trying various examples from Stackoverflow, I have not been able to successfully return the data ...

Sending a property as a parameter to a different component through Vue's router-link

I have a component designed to showcase recipes in a list format. Each recipe is rendered using a separate component from an array of recipes. I am currently attempting to pass the recipe object from one component to another component using Router-Link ...

Step by step guide to showcasing images dynamically in user interface

My current project involves displaying a screen with an HTML table and an image. The HTML table is fully dynamic. The Code Working Process When the user loads a page (with a URL), I render an HTML table in different parts as the page loads. I retrieve al ...

Numerical values are not considered by the JavaScript table filter

I'm having trouble with dynamically filtering the content. It works fine for the first two columns, but not for the third one. Maybe I need some additional JavaScript? Here is the link to my snippet: `https://www.w3schools.com/code/tryit.asp?filen ...