Is there a way to retain modal inputs even after the modal has been closed?

Within the initial modal, users are able to enter their name and hit a create button. Once the name is added, the next modal (modal 2) will appear.

I am seeking advice on how to retain the input from the first modal when a user clicks cancel in the second modal. How can I ensure that the name entered in the first modal, for example, "Kiiiiii," remains and is not erased? What is the correct term for this functionality?

Please disregard any discrepancies between names entered in modal 1 and modal 2. Ideally, they should be consistent.

For context, I am working with Angular. Could someone point me towards resources related to this issue? Thank you.

Any insights or suggestions would be greatly appreciated. Thank you.

I wish for the input to persist even if the cancel button is clicked.

https://i.sstatic.net/XIMhe.png

This image shows the design of the second modal

https://i.sstatic.net/EiYf3.png

Answer №1

I believe utilizing local storage is a great option. One approach is to create an object similar to a ViewModel to manage form data.

let obj = {
}

With this object, you can capture and store all form actions. Simply input your data into the object after completing the first modal.

obj.name = 'some field';

The idea is to keep track of form data within this object. By using JSON.stringify(), you can convert the object into a string and save it in local storage for persistence. Retrieving the data involves using JSON.parse() to revert the string back into an object. Once data is saved in modal 2, you can remove the string stored in local storage.

// helpful localStorage methods
localStorage.setItem();
localStorage.getItem();
localStorage.removeItem();

For more information on JSON and localStorage, check out these resources: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

Answer №2

To successfully integrate your modal, it is important to connect it with a model like this:

 export class EntryData{
        section:string="";
        name:string="";
        reprhrase:string="";
constructor(){}
    }

Then, add this data to your component.ts file:

entryData:EntryData;

Next, in the constructor of your component, make sure to initialize it like so:

this.entryData=new EntryData();

In your HTML code, utilize two-way binding for your input field:

<input type="text" [(ngModel)]="entryData.name" />

When you click on "Create Name," your model will be saved in the backend system. The changes you make to the model will persist even when you reopen the modal.

Answer №3

There are various possibilities in this situation,

  • One option is to utilize a service to manage and share global state values among different components.
  • Another approach could be using event emitters for passing values between components.
  • You may also consider options like input/output, viewchild, etc.

There might be other alternatives available as well, but their suitability will depend on how you execute these strategies.

I recommend checking out:

If you seek more specific advice, feel free to share your code :)

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

Could you guide me on how to utilize Sencha Touch for saving information in a JSON file?

I am in the process of creating a simple Sencha Touch application to become more comfortable with the platform. My goal is to have the store (currently set up as shown below) read and write data to/from a local JSON file. Additionally, I would like the sto ...

Creating images with LibCanvas

Currently, I am utilizing LibCanvas for canvas drawing on my HTML page. However, I am facing difficulty in drawing an image. Can someone provide assistance with this? UPDATE: The code snippet I am using is showing the image being drawn but then it disappe ...

Executing Node.js child processes: streaming stdout to console as it happens and running the processes one after the other

Details node: v9.4.0 I am looking for a solution to execute external commands sequentially while monitoring the stdout in real-time. The code snippet below demonstrates my attempt to retrieve all test cases from ./test/ and then running them one after ...

The parameter type Date cannot be assigned the argument type Moment

import { Moment } from 'moment'; import * as moment from 'moment/moment'; export class JobExecution { public startTime: Moment; constructor() { this.stepExecutions = []; } public get startTimeFormatted(): string { re ...

Determine if a property of an object is an empty string in React Native

I am facing a situation where I have an object that may or may not have an image URL. { id: 1, term: "Research", imageURL: "", definition: "is a way of thinking and understanding various aspects of a procedure ...

The code is throwing an error stating: "TransformStream" is not a recognized name

I'm encountering an issue with my socket.io code. It previously built without any problems, but now I am unsure about what changes have caused the build to fail. It seems that TransformStream, a native node library, is having trouble loading in Typesc ...

Troubleshooting an issue with two interdependent Knex MySQL queries within a router.get method

I am trying to retrieve and display data from two different database tables in a Node and Express route using JavaScript. I have successfully connected my Express app to the database using knex, and I am now using MySQL SELECT queries to extract informatio ...

Angular 13 does not currently have support for the experimental syntax 'importMeta' activated

Since upgrading to angular 13, I've encountered an issue while attempting to create a worker in the following manner: new Worker(new URL('../path/to/worker', import.meta.url), {type: 'module'}) This code works as expected with "ng ...

I am interested in creating a class that will produce functions as its instances

Looking to create a TypeScript class with instances that act as functions? More specifically, each function in the class should return an HTMLelement. Here's an example of what I'm aiming for: function generateDiv() { const div = document.crea ...

What is preventing AJAX from displaying the updated JSON content?

I'm brand new to JavaScript and I'm trying to send an ajax request to retrieve the contents of a JSON file and display it in a div. Here's the code I currently have: <!DOCTYPE html> <html> <head> <script src="https: ...

Is it possible to access the service and 'self' directly from the HTML template?

When working with Angular 6, one method to access component properties from a service is to pass 'self' to the service directly from the component. An example of this implementation is shown below: myComponent.ts public myButton; constructor(p ...

Discovering the deployed version of your Angular application through the powerful combination of Jenkins and Docker

My application is built on Angular and calls Python (FastAPI) APIs. The deployment process involves using Jenkins to build Docker images and push them to the server. The Jenkins file in the Angular project (current version 0.3) looks like this: pipeline { ...

React: maintaining referential equality across renders by creating closures with useCallback

I want to make sure the event handling function I create in a custom hook in React remains referentially equal across renders. Is it possible to achieve this using useCallback without specifying any variables it closes over in the dependencies list? Will o ...

Is it necessary to utilize Express or another library in order to create a server with Node.js that exclusively serves up JSON data?

As I delve into testing node.js, a multitude of questions have flooded my mind. My goal is to create a server for a mobile app that simply provides the data I require from a database. While this may not be overly complex, most people I've observed use ...

Utilizing Angular to Fetch JSON Data from a Server

I am currently retrieving data from a JSON file that resides on a server and is updated regularly. It's crucial for me to access this JSON content in order to consistently showcase the most recent information on my website. At present, I have stored ...

Is it possible that Angular 1.0.8 code is incompatible with version 1.6.5?

Just started dabbling in angular-js today and decided to experiment with some older examples. I have a backend api that provides me with a json list. I am trying to fetch this data and display it on my webpage. The examples I found were built using versi ...

Having trouble containerizing Angular app - sh: ng-openapi-gen: command not recognized

I'm in the process of dockerizing an Angular application using Docker and Docker compose. Here is how my Dockerfile looks: FROM node:13.10.1-alpine AS build RUN mkdir /app RUN apk update && apk add --no-cache git WORKDIR /usr/src/app COPY p ...

Retrieve the screen dimensions, including the source code panel and debug panel, utilizing jQuery

$(window).height(); $(window).width(); I am currently utilizing these JavaScript codes to obtain the screen size. However, I have noticed that when the source code panel/debug panel is open, it affects the accuracy of the screen size results. Is there a w ...

Using a Firebase forEach loop to push items into an array and returning the completed array

At the moment, I am facing difficulties with nodejs (v6.14.0) because I made a post request that needs to complete some tasks before returning a valid response. The issue is that I receive an empty array as a response initially (the response becomes valid ...

Is the username you want available?

I am facing a challenge in my registration form validation process where I need to verify the username input using javascript and flask in python, but the implementation is unclear to me. The requirement is to utilize $.get in the javascript segment along ...