Ways to retrieve a Class Level Variable within the onCellValueChanged function in agGrid

Exploring Angular with Typescript for the first time. I'm trying to access Class Level Variables within the onCellValueChanged event of agGrid, but encountering the following error:

Cannot read property 'updateDict' of undefined

Here's my TypeScript snippet:

import { Component, OnInit } from '@angular/core';
import { APICallService } from '../../.././PortalServices/webapicall.service';

@Component({
    selector: 'master-gender',
    templateUrl: 'app/PortalPages/MasterPages/GenderMaster/gender.component.html',
    providers: [APICallService],
})

export class GenderComponent implements OnInit {
    private updateDict = new Map();
    genderRowData: [];  

    constructor(private genderService: APICallService) {
    }

    ngOnInit() {
        this.genderService.getApiCall('getallgenders')
            .subscribe((rowData) => this.genderRowData = rowData,
                (error) => { alert(error) }
            );   
    }

    columnDefs = [
        {
            headerName: 'Gender Name', field: 'Name',          
            cellEditor: 'agLargeTextCellEditor',
            cellEditorParams: {
                maxLength: '100',
                cols: '20',
                rows: '2'
            },
            onCellValueChanged: this.genderCellValueChanged           
        },
    ]

    genderCellValueChanged(event: any) {
        let oldData = '';

        //Error coming from below line

        if (typeof (this.updateDict[event.data.GenderId + ',' + event.colDef.field]) != 'undefined')
            oldData = this.updateDict[event.data.GenderId + ',' + event.colDef.field].OldData;
        else
            oldData = event.oldValue;
        console.log(oldData);
    }
}
  1. Any suggestions on how to resolve this issue?
  2. Can "genderCellValueChanged" method be moved to a child component (separate .ts file)? If so, could you provide detailed steps?

Answer №1

If you want to specify the context of `this`, make sure to utilize the `bind` method as shown below:

onCellValueChanged: this.genderCellValueChanged.bind(this)

I trust this explanation will be beneficial.

Answer №2

There are no values inside the updateDict, but it is defined as an Object.

Take a look at this sample

if (typeof this.updateDict !== 'undefined')
    {
       console.log(this.updateDict.length);
        this.updateDict.set('1', 'str1');   
        console.log(this.updateDict.length);
    }

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

Utilize generics to define the data type of the output

Within my Angular service, there is a method that retrieves data from Sync Storage: getFromSyncStorage(key: string): Promise<Object | LastErrorType> { return new Promise(function (resolve, reject) { chrome.storage.sync.get(key, function ( ...

Exploring the capabilities of Vue combined with Typescript and Audio Worklets

I've encountered a challenge with configuring Vue to compile audio worklets. Specifically, I am facing a similar issue to this problem that has already been resolved, but using Typescript instead of JavaScript. My approach was to include the ts-loader ...

Can Typescript Be Integrated into an AngularJS Application?

I have been thinking about the optimal timing and scenario to implement Typescript in an AngularJS project. While I have come across examples of TS being used in a Node, Express, Mongo backend, I am particularly intrigued by how well TS integrates with A ...

What methods can I use to prevent the addition of new values and duplicates within angular mat-chips?

I am utilizing angular 9 mat-chips and I am seeking a way to prevent adding new values in the input field, allowing only items from the autocomplete list to be added. For instance, if 'abc' is typed, which is not in the autocomplete list, pressin ...

After installing the latest version of Node.js, the stability of the Ionic environment

After updating nodejs from v8.1 to v12, my ionic environment is experiencing instability. Any suggestions on what needs to be updated? [abc]$ ionic cordova emulate android When running ng run app:ionic-cordova-build --platform=android, an unhandled exce ...

The input tag loses focus after its value is updated using a class method in Angular 8.x

Currently, I am working on integrating a credit card payment method and formatting its number through specific methods. Here is how it is done: HTML <div class="form-group" *ngFor="let formField of cardFields; let cardFieldIndex = index;"> ...

Tips for setting unique base_url for separate environment instances in angular2

My index.html file in Angular2 has the following content: When I build it in default mode (development mode) using npm run build, I want it to have: <base href="/"> However, when I build it in production mode (npm run build --target=production), I ...

Guide to incorporating Bootstrap icons into an Angular application through programming techniques

Have you checked out the official bootstrap documentation for information on how to use their icons? https://i.stack.imgur.com/N4z2R.png I'm currently trying to understand the package and its usage. However, none of the options in the documentation ...

What could be the reason for the component not receiving data from the service?

After attempting to send data from one component to another using a service, I followed the guidance provided in this answer. Unfortunately, the data is not being received by the receiver component. I also explored the solution suggested in this question. ...

Exploring type definition for function arguments in TypeScript and React

There is a high-order component (HOC) designed to store the value of one state for all input and select elements. The output function accepts arguments ({text: Component, select: Component}). An error is displayed while typing an argument: TS2322: Type &ap ...

When attempting to access http://localhost:3000/traceur in Angular 2 with the angular-in-memory-web-api, a 404 (Not Found) error

Hello, I'm encountering an issue with angular-in-memory-web-api. I have attempted to use angular2-in-memory-web-api in SystemJS and other solutions, but without success. I am currently using the official quickstart template. Thank you for any assistan ...

Adjust the Angular menu-bar directly from the content-script of a Chrome Extension

The project I've been working on involves creating an extension specifically for Google Chrome to enhance my school's online learning platform. This website, which is not managed by the school itself, utilizes Angular for its front-end design. W ...

Utilizing Interface Merging: Determining the Appropriate Instance Type for Field Types

I am in need of writing a definition file for an external library. I have augmented a class using interface merging and there are situations where a field of the library class is of the same type as the instance itself. Here is a snippet of demo code: // ...

MatDialog displaying no content

I found this tutorial on how to implement a simple dialog. The issue I'm facing is that the dialog appears empty with no errors in the console. Even after making changes to my dialog.component.html or dress-form.ts, nothing seems to reflect in the o ...

The 'autoComplete' property cannot be found within the 'IntrinsicAttributes & InputProps' type

I'm currently utilizing Typescript and React, but encountering the following error: Property 'autoComplete' is not found on type 'IntrinsicAttributes & InputProps'. This is how I am using the component: <Input ...

Make leaflet function operate synchronously

There seems to be an issue with calling the setMarker() function within another function, as the markers are not being set. It's possible that this is due to the asynchronous nature of the setMarker() function because of the Promise it uses. getCities ...

Exploring type delegation in TypeScript

Here is a scenario where I am using the builder pattern in my code: export class ValidationBuilderBase implements IValidationBuilder { public isRequired(): IValidationBuilder { const validationResult = Validators.required(this.baseControl); ...

Issue encountered when attempting to invoke a service from an Angular component within an office.js dialog

Our application utilizes Angular 5 and integrates Office.js to interact with Microsoft Office Word documents. Step 1: We use office displayDialogAsync to load the component. Step 2: Inside the attribute-users component, an HTTPS GET request is made to re ...

Guide to importing a custom component into an Ionic 4 page

In Ionic 4, I have a page called IntroPage that utilizes a custom component named intro-task. When trying to use intro-task, Angular throws an error: Can't bind to 'active' since it isn't a known property of 'intro-task'. 1. ...

Insert a new row into the table with a value that is taken from the input fields on the form

view image details I am working on a form that consists of 4 fields: Name, Last name, email, and role. There is also a button. When the button is clicked, the input from the above form should be added as a new row in a table below. ...