How to extract the selected item from an ion-list in Ionic 2 and connect it to an input field

Within my code, I have an array defined as follows:

public items = ['item1', 'item2', 'item3'];
This array is utilized in the view using the following structure:

<ion-slide> <ion-list inset>
    <ion-item *ngFor="let item of items" (click)="getItem(title)">
      {{ title }}
    </ion-item>
</ion-list> </ion-slide>

The function getItem(title) is written like this:

getItem(item) {
console.log(item);
this.slider.slideNext();
}

This function allows me to navigate to the next slide and pass the selected item as a parameter. On the second slide, the HTML structure looks like this:

<ion-slide> <ion-item>
          <ion-label stacked>Label</ion-label>
          <ion-input type="text">{{ item }}</ion-input>
</ion-item> </ion-slide>

The goal here is to display the chosen item from the first slide within the input field on the second slide.

Answer №1

To implement this functionality in your component, start by declaring a variable to hold the selected item data. Then, make sure to display this variable within the slide.

export class MyComponent {

 public selectedData: any;

 constructor(){}

 getData(data) {
  // save data into the variable
  this.selectedData = data;
  console.log(data);
  this.slider.slideNext();
 }


}

Next, modify your HTML to include the following:

<ion-slide> 
  <ion-item>
   <ion-label stacked>Label</ion-label>
   <ion-input type="text" [(ngModel)]="selectedData"></ion-input>
  </ion-item> 
</ion-slide>

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

Anticipated the object to be a type of ScalarObservable, yet it turned out to be an

When working on my Angular project, I utilized Observables in a specific manner: getItem(id): Observable<Object> { return this.myApi.myMethod(...); // returns an Observable } Later, during unit testing, I successfully tested it like so: it(&apos ...

Angular: Display a null value retrieved from an asynchronous data source

When retrieving data from the backend (Node.js/Express.js + Oracledb), there are null values present. I need to display "Null" in the HTML table wherever these null values exist. Is there a way to achieve this? This is my code: .component.html <div cl ...

Issues arise with Jest tests following the implementation of the 'csv-parse/sync' library

Currently utilizing NestJs with Nx.dev for a monorepo setup. However, I've come across an issue after installing csv-parse/sync - my Jest tests are now failing to work. Jest encountered an unexpected token Jest failed to parse a file due to non-stand ...

ReactJS: error occurs when trying to fetch data and encountering issues with reading properties

I am currently attempting to initiate an API call (a GET request) in order to download a document. However, I am encountering an error when making the API call: TypeError: Cannot read properties of undefined (reading 'payload') const printPin ...

Refreshing a page with a 404 error in Angular 2 while in production mode and without the useHash configuration

I've encountered an issue while using Angular 2 without the useHash feature. When trying to visit the URL directly in a browser, I'm getting a 404 not found error. I have searched extensively and attempted various solutions including: Adding L ...

The TypeScript reflection system is unable to deduce the GraphQL type in this case. To resolve this issue, it is necessary to explicitly specify the type for the 'id' property of the 'Address'

import { ObjectType, ID, Int, Field } from 'type-graphql'; @ObjectType() export default class Address { @Field(type => ID) id: String; @Field() type: string; @Field() title: string; @Field() location: string; } More informa ...

The utilization of a Typescript Generic for basic addition is not producing the desired results

This issue feels incredibly insignificant, but for some reason I can't seem to figure out why it's not functioning correctly. It seems like the return type should match since I am simply concatenating or adding based on whether it's a number ...

Deploying Angular5 application to various customer bases

In the process of developing an angular application intended for deployment across multiple clients, I have encountered a challenge. Each client will host the application on their own servers, resulting in different Services' URLs for each one. The co ...

The data type of Subscription: prototype, NOT ASSIGNED

I am encountering the following error when attempting to return a subscription object. Error Message:-------- Type 'Subscription' does not have the prototype and EMPTY properties that are expected from type 'typeof Subscription' Here ...

What is the process for creating a personalized tag?

Having created a component similar to the one below. In MyButton.tsx import React from 'react'; import { Button } from '@material-ui/core'; import { Theme, makeStyles, createStyles } from "@material-ui/core/styles"; interface IMyButt ...

How can you trigger the activation of a component in Angular 2 without needing to navigate to the actual

Currently, I am developing an application that includes a cart feature as a separate module component. When clicking the button to add items to the cart, I have implemented a subscription event in the cart component: ngOnInit(): void { this._cartSer ...

Angular Route Protection Using Auth Guard

Implementing auth-guard to secure my routes has been a challenge for me. One issue I encountered is that if a user is already logged in, they should not be able to access the sign in or sign up page. Instead, they should be redirected to the default page. ...

Utilizing a Dependency Injection container effectively

I am venturing into the world of creating a Node.js backend for the first time after previously working with ASP.NET Core. I am interested in utilizing a DI Container and incorporating controllers into my project. In ASP.NET Core, a new instance of the c ...

Vue component prop values are not properly recognized by Typescript

Below is a Vue component I have created for a generic sidebar that can be used multiple times with different data: <template> <div> <h5>{{ title }}</h5> <div v-for="prop of data" :key="prop.id"> ...

Issue in Ionic 2: typescript: The identifier 'EventStaffLogService' could not be located

I encountered an error after updating the app scripts. Although I've installed the latest version, I am not familiar with typescript. The code used to function properly before I executed the update. cli $ ionic serve Running 'serve:before' ...

Dealing with API responses in Angular 2

Hello there! I am a beginner in Angular 2 and might ask some basic questions, so please bear with me. I am struggling to understand how to handle an API response. Below is my NodeJS Server API function (which has been checked and is working fine): router ...

The medium-zoom feature is currently experiencing issues with functionality within Angular version 13

I've been attempting to incorporate medium-zoom functionality into my project using https://www.npmjs.com/package/medium-zoom Here are the steps I took: ng new medium_zoom_test (Angular 13) with routing & css npm install medium-zoom The image is ...

Issue with Angular 7 Universal: components inside are failing to display

I have successfully implemented Angular 7 Universal with dynamic server-side rendering. However, I am facing an issue where dynamic components within the main component being rendered on the server are not rendered themselves. Here is an example of the re ...

Developing a custom typography attribute for themes in Material-UI with TypeScript

I have encountered an issue with my code where the palette works fine, but there seems to be a problem with the typography section without any errors being thrown: Here is a breakdown of the steps I took: Firstly, I imported the module import "@mui ...

Tips for integrating a custom handler to the close icon in Material UI TextField component

In my Reactjs/Typescript project using Material UI, I have a search input component rendered with TextField. The built-in "x" icon clears the input value, but I want to create a custom handler for making an API call when the search value is deleted. I&apo ...