Utilize the input fields within an Ionic form for multiple purposes

I have implemented this form method in Ionic:

After filling out the form and submitting it, a new slide appears. However, upon restarting the app, everything resets back to the first slide. How can I address this issue? I would like to be able to save these input values in the slides for future use...

Here are some snippets of the code:

.html:

<ion-slide *ngFor="let slide of slides">
  <ion-header>
    <ion-navbar>
      <ion-title text-center class="persian pdtop" > <ion-icon name='{{slide.icon}}'> </ion-icon> {{slide.ttl}}</ion-title>
    </ion-navbar>
  </ion-header>
  <ion-content padding>
    <img [src]="slide.image" class="slide-image"/>
    <h2 class="slide-title persian2" [innerHTML]="slide.title"></h2>
    <p class="persian" [innerHTML]="slide.description"></p>
  </ion-content>
</ion-slide>
<form [formGroup]="todo" (ngSubmit)="logForm()">
  <ion-item>
    <ion-label>Title</ion-label>
    <ion-input type="text" formControlName="title"></ion-input>
  </ion-item>    
  <ion-item>
    <ion-label>Icon</ion-label>
    <ion-input type="text" formControlName="icon"></ion-input>
  </ion-item>    
  <ion-item>
    <ion-label>Header</ion-label>
    <ion-input type="text" formControlName="ttl"></ion-input>
  </ion-item>    
  <ion-item>
    <ion-label>Image</ion-label>
    <ion-input type="text" formControlName="image"></ion-input>
  </ion-item>
  <ion-item>
    <ion-label>Description</ion-label>
    <ion-textarea formControlName="description"></ion-textarea>
  </ion-item>
  <button ion-button type="submit" [disabled]="!todo.valid">Submit</button>
</form>

.ts:

// ...
export class HomePage {
  private todo : FormGroup;
  constructor(public nav: NavController, private formBuilder: FormBuilder ) {
    this.todo = this.formBuilder.group({
      title: ['', Validators.required],
      description: [''],
      icon: [''],
      ttl: [''],
      image: [''],
    });
  }

  logForm(){
    console.log(this.todo.value);
    this.slides.push(this.todo.value);
  }

  slides = [
    {
      title: "HBD",
      description: "The <b>Ionic Component Documentation</b> showcases a number of useful components that are included out of the box with Ionic.",
      image: "",
      ttl: "no.1",
      icon: "heart",
    },
    {
      title: "What is Ionic?",
      description: "<b>Ionic Framework</b> is an open source SDK that enables developers to build high quality mobile apps with web technologies like HTML, CSS, and JavaScript.",
      image: "",
      ttl: "my love",
      icon: "heart",
    }
  ];
}

//...

Upon submitting the form, a new slide is displayed. Unfortunately, after restarting the app, the data reverts back to the initial state. Is there a way to retain these input values in the slides for persistent usage?

Answer №1

Upon each restart of your app, the Slides component is reset and initialized again, with the default setting being on the first slide (index=0).

To ensure that a specific slide is maintained even after restarts, it is advisable to save the index of that slide in a database, LocalStorage, or provider. This can be done by using the getActiveIndex() method whenever slide changes occur.

If you wish to start the Slides component on a particular slide (e.g., the one saved), you can utilize the initialSlide property and assign it the desired slide's index number.

For example, if you intend to initialize the Slides on slide index=2, the code would look like this:

<ion-slides initialSlide="2">
  <ion-slide>
  </ion-slide>
<ion-slides>

For detailed information on the Slides component, I recommend referring to the official Ionic Documentation provided here: https://ionicframework.com/docs/api/components/slides/Slides

I trust this guidance will assist you in resolving any issues you may encounter.

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

The use of the keyword 'await' was not anticipated

I'm currently working on a react-native application and I've encountered an issue with my forEach iteration. I decided to use await to pause for the result, but instead I keep receiving an error message stating "Unexpected reserved word 'awa ...

Adding elements to an array using JavaScript

What is the correct way to add new values to an array like this? json = {"awesome":"45.22","supercool":"9876"} I attempted json.push("amazingness":"45.22");, but unfortunately it was not successful. ...

Changing visibility when class is removed with display: none

I have a collection of div elements, some of them are currently hidden using the class "not-updated", while others are visible. .not-update{ display: none } With certain AJAX calls, some of these hidden divs may become visible by removing the ...

Nodejs restricts the user from performing the action

Currently, I am utilizing Nodejs with Expressjs and encountering an issue when trying to run the project. The connection is established successfully but when attempting to access the URL (localhost:3001/api/plans), an error appears in the console: MongoSer ...

The issue persists where $.ui remains undefined despite the jQuery UI library being successfully loaded

I am encountering an issue with my JavaScript code. I have a page that contains a self-invoked function which runs when the page loads. This page also incorporates both jquery and jquery ui libraries, with jquery being included first followed by jquery ui. ...

Tips to Prevent PostBack in Asp.net MVC4

I have a form where I need to bind some values to two different dropdowns and save the user's selected value. I am using RequiredIf attributes, which work fine. However, if the user forgets to select a value, an error message is shown. The issue arise ...

Searching for patterns in text to identify non-blank characters with specified limitations

I'm looking to extract text surrounded by % symbols without any whitespace between them. For example, this should match: %link% But this shouldn't: %my link% An easy regex solution would be: /%\S*%/g However, there's a requirement t ...

Steps for retrieving the output of a sequelize query that is enclosed within a function

I am currently in the process of developing a platform that generates a distinct URL for each user. The functionality is as follows: Firstly, I check the database to determine if the user already possesses a URL, and if so, return it. If the user ...

Use the filter method to sort through an array of objects by using another array

I need to filter an array using two criteria: one is straightforward (==1) and the other involves an array. In this example, I want to filter where level = 0 or name includes ['B','S']. [ {id: 1, level: 0, name: 'A'}, {id: 2, ...

unable to execute PHP code

I am attempting to execute a PHP file that will update a database. On Chrome, I am encountering this error: https://i.sstatic.net/3ruNL.png This is the code I have in my index.html file: <!DOCTYPE html> <html> <body> <input type ...

Error message 2339 - The property 'toggleExpand' is not recognized on the specified type 'AccHeaderContextProps | undefined'

When utilizing the context to share data, I am encountering a type error in TypeScript stating Property 'toggleExpand' does not exist on type 'AccHeaderContextProps | undefined'.ts(2339). However, all the props have been declared. inter ...

Using event.target to pass HTML form data to FormData is causing an error stating that the Argument of type 'EventTarget' cannot be assigned to a parameter of type 'HTMLFormElement'

Looking to extract data from a form and store it in FormData: const handleSubmit = (e: FormEvent<HTMLFormElement>) => { e.preventDefault(); const formData = new FormData(e.target as HTMLFormElement); const value = formData.get(' ...

Issue with Node.js Stream - Repeated attempts to call stream functions on the same file are not being successful

When a POST request with multipart/form-data hits my server, I extract the file contents from the request. The file is read using streams, passed to cvsParser, then to a custom Transform function that fetches the resource via http (utilizing got) and comp ...

Having a text input that is dependent on choosing a particular item from a list

As someone who is new to VueJs and JS, I have a question regarding my page setup. I want a text input box to only be visible when the user selects the document type 'Write Individual'. However, my current syntax seems to be incorrect as it's ...

Ways to resolve the issue of setAttribute value not appearing on the UI

When using the setAttribute method to set a value in the DOM, why is it not being displayed in the user interface? https://i.sstatic.net/hZRgX.png ...

Message indicating NoteContext is not defined

In my React project, I first created a NoteContext and then initialized an array using the useState hook. This array was imported into the Notes component where I used .map to display the titles of the objects in the array. However, when running the code, ...

Enhance your webpack workflow with the plugin API to dynamically update asset content and regenerate hashes

For a project I'm working on, I've developed a plugin that handles the swapping of content in a specific JSON file after all modules are bundled. This is achieved through 2 steps: a loader that replaces the content with a placeholder, and the plu ...

history.js - failure to save the first page in browsing history

I have implemented the history.js library to enable backward and forward browser navigation on an AJAX products page triggered by clicking on product categories. While I have managed to get this functionality working smoothly, I am facing a particular iss ...

Using AngularJS to Filter Array Elements

Here is a sample of the JSON data I am working with: products: [{ id: "1", model: "123", price: "123", spec: "213", image: "", brand: "213", category: "1", sub_category: "1", color: "1", size: "1", order: "1", ...

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. ...