Loop through the information retrieved from the alertController

I'm currently facing an issue regarding accessing data in my alert controller

let alert = this.alertCtrl.create({
  title: 'Edit Index',
  inputs:this.customIndexes,
  buttons:[
    {
      text: 'Cancel',
      role: 'cancel',
      handler: data=> {
        console.log('Cancel clicked');

      }
    },
    {
      text: 'Save',
      handler: data=>{
        console.log(data);
        /*for(let item of this.customIndexes)
        {
          this.customIndexes[item.name].value = data[item.name];
        }*/
        this.postEditedIndex(this.customIndexes);
      }
    }
  ]
});
alert.present();

When the user presses the save button, how can I retrieve the data?

My inputs are dynamic with the specified array structure like

customIndexes: { name: string, value: string, placeholder: string }[] = [];]

The function displays all fields that are populated into custom indexes, but how can I access them from the data object in the save button handler?

Answer №1

The issue you are encountering is related to the scoping of Object literals. To better understand this, take a look at this example: http://jsfiddle.net/1b68eLdr/76008/

class Test1 {
  name = "Name1";

  getName() {
    let test2 = {
      name: "Name2";
      getName2: ()=> {
        return this.name;
      }
    }
    return test2.getName2();
  }
};

let test1 = new Test1();
let name1 = test1.getName()

As a result, name1 will equal "Name2". This occurs because when you define a function within another object, it inherits that object's 'this' instead of the outer scope's 'this'. A common workaround for this is to store 'this' in a different variable (typically named self). For example: http://jsfiddle.net/5nup6cgx/

class Test1 {
  name = "Name1";

  getName() {
    let self = this; //<<<< NOTE HERE
    let test2 = {
      name: "Name2";
      getName2: ()=> {
        return self.name; //<<<< AND HERE
      }
    }
    return test2.getName2();
  }
};

let test1 = new Test1();
let name1 = test1.getName()

Therefore, what you likely intended to do was:

let self = this; //<<<<< NOTE HERE
let alert = this.alertCtrl.create({
  title: 'Edit Index',
  inputs:this.customIndexes,
  buttons:[
    {
      text: 'Cancel',
      role: 'cancel',
      handler: data=> {
        console.log('Cancel clicked');

      }
    },
    {
      text: 'Save',
      handler: data=>{
        console.log(data);
        for(let item of self.customIndexes)  //<<<<< AND HERE
        {
          self.customIndexes[item.name].value = data[item.name];  //<<<<< AND HERE
        }
        self.postEditedIndex(self.customIndexes);  //<<<<< AND HERE
      }
    }
  ]
});
alert.present();

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

How to separate rows in GridView with a divider

I created a data table using a GridView and want to add a horizontal divider beneath each row, similar to the design guidelines from Google. I've searched for information on how to implement this but haven't found anything useful. Any suggestions ...

An issue occurred in the modal window following the relocation of project files

I encountered an issue with the modal in my Nativescript project after rearranging a few project files, including the modal. I updated the imports and deleted any compiled JavaScript files to ensure that my project could recompile correctly. Although I&ap ...

Nested forwardRef in React is a powerful feature that allows

Within my React application, specifically utilizing typescript, I have implemented a form using react-hook-form to handle all the necessary logic. Afterwards, I proceeded to customize the select element with various CSS and additional features. To simplif ...

Error with Chakra UI and React Hook Form mismatched data types

Struggling with creating a form using ChakraUI and React-Hook-Form in TypeScript. The errors seem to be related to TypeScript issues. I simply copied and pasted this code from the template provided on Chakra's website. Here is the snippet: import { ...

A versatile function catered to handling two distinct interface types within Typescript

Currently, I am developing a React application using TypeScript. In this project, I have implemented two useState objects to indicate if an addon or accessory has been removed from a product for visual purposes. It is important to note that products in thi ...

Is there a way to detect changes in a Service variable within an Angular component?

One of my components contains a button that activates the showSummary() function when clicked, which then calls a service named Appraisal-summary.service.ts that includes a method called calc(). showSummary(appraisal) { this.summaryService.calc(appraisal ...

What are the reasons for deprecating bindToController in Typescript?

When I am creating an AngularJS directive using TypeScript, I typically use the bindToController property to bind parameters to the controller for easy access. export class MyDirective implements IDirective { controller = MyController; controllerA ...

What is the process for retrieving an element from component interaction?

Is there a way to dynamically change the background color based on component interaction? I am looking for a method to compare the target element with the current element. For example, here is a hypothetical scenario: <span [style.background]=" ...

Slow scrolling issue in Recyclerview

I'm currently experiencing lag while scrolling with the RecyclerView Gridlayout. Since I'm using the RecyclerView.Adapter, the ViewHolder is already in place. In addition to that, there are no conditions in the onBindViewHolder method or the on ...

How to declare and initialize a variable in Angular 2 using TypeScript

I'm currently working with angular 2 and I'm having trouble understanding how to set a variable. The variable _isLoading is being set in "this.observable.filter((event)" but it doesn't seem to change in the template. This is my TypeScript co ...

How come JavaScript variables are able to persist on JQuery Mobile multi-page templates on desktop browsers, but not on mobile browsers?

My website was created using the jQuery Mobile multi-page template. While testing it on Chrome desktop, I noticed that my JavaScript variables (comics and checkedItems) retain their values when navigating between pages. However, on mobile devices, these ar ...

typescript's JSON.stringify function includes internal fields but omits public interface values

I'm currently grappling with some confusion surrounding serialization in TypeScript using JSON.stringify and interfaces. My goal is to create an export format for serializing certain objects back to their server-side representation, focusing solely on ...

Problem with Change Detection in Angular 2

I am currently utilizing Angular 2.1.1 for my application. Situation Let's consider a scenario where two components are interacting with each other. The component DeviceSettingsComponent is active and visible to the user. It contains a close button ...

Tips for tracking the evolution of changes to an array within React State

Experiencing challenges with saving the history of updates and modifications on a State. I have an object called "Journey" which includes a list of workshops (another array). Whenever I update my list of workshops, I aim to create a new array that captures ...

Crafting a template for mixing up images and quotes without any specific connection - here's how!

I came across some interesting templates that can generate random quotes and others that create random pictures. My goal is to develop a template that generates both a random quote and a random picture, similar to this example, without any relation betwee ...

"Encountering issues with the functionality of two Angular5 routers

main.component.html [...] <a routerLink="/company-list">Open</a> [...] <main> <router-outlet name="content"><router-outlet> </main> [...] app.compoment.html <router-outlet><router-outlet> app.routing.modu ...

Tips for assigning a numerical value to an array and showcasing it in a listview

I have a dilemma involving two activities. The first activity displays a list of items using a ListView. In the second activity, I also want to display a list of items, but the content will vary based on the position of the element selected in the first ac ...

Invoke index functions within a component

I have a widget/component written in Angular 4 within the index.html file. Before and after this angular app, there are various HTML elements due to the nature of it being an additional component for the website. The head section of the index file include ...

Jasmine Destiny - Error Encountered: macroTask 'setTimeout': unable to switch to 'active' state, expecting 'planned' state, but it was 'notScheduled'

I am currently using Angular 7 with the Zone.js version of approximately ~0.8.26. Inside my test.ts file, I have included the import statement for 'zone.js/dist/zone-testing'. Below is a snippet from my spec file: import { HttpClientTestingModul ...