What is the method to insert a new <input> element after the last input field has been filled in

I recently started working on a form using StackBlitz, but I've hit a roadblock and need some guidance on how to proceed.

My goal is to achieve a similar effect like the one shown in this gif: https://i.stack.imgur.com/76nsY.gif and I'd like to implement it following the approach used in this code snippet: Github - RESTer

I attempted to use the (change) method, but it only triggers after pressing Enter, which doesn't meet my requirements.

Answer №1

To manage the input event on the final line, you can do the following:

<div *ngFor="let header of headers; let i = index; let isLast = last" fxLayout="row" fxLayoutGap="25px">
  <mat-form-field fxFlex="25">
    <input matInput placeholder="Name" type="text" name="name-{{i}}" value="{{header.name}}" 
           (input)="handleInput(isLast)">
  </mat-form-field>
  <mat-form-field fxFlex>
    <input matInput placeholder="Value" type="text" name="value-{{i}}" value="{{header.value}}">
  </mat-form-field>
  ...
</div>

utilize the handleInput function, which inserts a new entry upon the first modification of the input content:

export class DynamicformComponent implements OnInit {

  public headers: any[] = [];

  ngOnInit() {
    this.headers = [
      { name: 'Accept-Encoding', value: 'gzip' },
      { name: 'Accept-Charset', value: 'utf-8' },
      { name: 'User-Agent', value: '' },
      { name: 'Accept', value: 'text/plain' },
      { name: 'Cookie', value: '' },
      { name: '', value: '' }
    ];
  }

  handleInput(isLast: boolean) {
    if (isLast) {
      this.headers.push({ name: '', value: '' });
    }
  }

  eraseInput(index) {
    this.headers.splice(index, 1)
  }
}

View the demonstration on this stackblitz.

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

What is the best way to access a web.config key using Typescript?

Is there a way to retrieve key values in an Angular 2 application using TypeScript? add key="localPath" value="http://localhost:618/" add key="serverPath" value="http://api.azure.net/" I am looking to access the values of "localpath" and "serverpath" in ...

Troubleshooting problem with Webpack and TypeScript (ts-loader)

Seeking help to configure SolidJS with Webpack and ts-loader. Encountering an issue with return functions. What specific settings are needed in the loader to resolve this problem? import { render } from "solid-js/web"; const App = () => { ...

Reassigning Key Names and Types Based on Conditions

How can I modify object key names and properties in a way that allows existing keys and properties to remain the same or be modified (remapped)? My current approach does not properly handle mixed cases: export const FUNC_ENDING_HINT = "$func" as const; ty ...

Having trouble implementing object type switching in Typescript

While in the process of developing an angular application, I stumbled upon a peculiar issue. Some time ago, I crafted this piece of code which performed flawlessly: selectedGeoArea: any receiveStoreEvent(event) { switch (event.constructor) { ca ...

Encountering a TypeScript error in Next.js: The 'Options' type does not align with the 'NavigateOptions' type

My code snippet: import { useRouter } from 'next/navigation'; interface Options { scroll: boolean; } const Component = () => { const router = useRouter(); const updateSearchParams = () => { const searchParams = new URLSearchPa ...

Issue with Angular Routing not functioning properly when including an ID as a parameter in the URL

Just starting out with Angular and I'm currently working on my app.module.ts file. Here's what I have: RouterModule.forRoot() { ... { path : "posts/:id", component: PostprofileComponent }, { path : "p ...

Angular 4 in combination with ngx-datatable is showing a 404 error for the @swimlane/ngx-datatable package

Just starting out with Angular and I kicked things off by running this command: git clone https://github.com/angular/quickstart appName I've made the upgrade to Angular 4 and everything seems to be in order. Here's the output I got after running ...

Upon installation of a new component, Angular components are automatically removed from the system

I am currently working on an asp.net core web application with angular 5 and I need to generate some Barcodes for printing. Fortunately, I came across ngx-barcode, which is a barcode component specifically designed for Angular4+. To install it, I used the ...

We are unable to create a 'Worker' as Module scripts are not yet supported on DedicatedWorkers in Angular 8

Error An error has occurred in the following files: node_modules/typescript/lib/lib.dom.d.ts and node_modules/typescript/lib/lib.webworker.d.ts. Definitions of various identifiers conflict with those in other files, leading to modifier conflicts and dup ...

Is there a way to instruct Alexa/Jovo to incorporate just one render document in its response?

Within my project, there are multiple outputs, but I am specifically focused on an output that presents 2 directives: an APL and an APLA render document. My component is set up to handle this in the following way: @Handle({ global: true, prioritiz ...

Can we verify the availability of an Angular library during runtime?

I am in the process of creating a custom Angular library, let's name it libA, which has the capability to utilize another Angular library, named libB, for an optional feature. Essentially, if the Angular application does not include libB, then the fea ...

How can we stop the parent modal from closing if the child component is not valid?

I have a main component with a modal component that takes another component as a parameter. The child modal component has some logic where I need to check if the child component is valid before closing the modal. const MainComponent: FC<IProps> => ...

Encountering the following error message: "E11000 duplicate key error collection"

Currently, I am in the process of developing an ecommerce platform using the MERN stack combined with TypeScript. As part of this project, I am working on a feature that allows users to provide reviews for products. The goal is to limit each user to only o ...

Is there a distinction between Entity[] and array<Entity> in TypeScript?

Everything in the title, for example people: Person[]; people: Array<Person>; What sets them apart? Is there a preferred approach? Note: I couldn't find any guidance on this and I've encountered both in code. ...

Launching Angular 2 Application on Azure Cloud

I have recently been diving into the world of Angular 2 and Azure. Following the steps outlined in the Angular 2 Tutorial, I was able to successfully run my application locally without any issues. After deploying the app to Azure, I encountered a problem. ...

Utilizing MongoDB query for geoLocation with maxDistance parameter

Customer location: customerCoordinates: [83,24] stores: { id:1, location: {coordinates:[85,44]...} maxRadiusDelivery: 2000 //meters }, { id:2, location: {coordinates:[82,34]...} maxRadiusDelivery: 100 //meters } Query: db.wh.find({ 'locati ...

Comparing setInterval with Observable's timer method, which is the superior choice for setting up timers?

In order to create a timer in Angular, I am currently using Observable as shown below: timer: Observable<any> = Observable.timer(0, 990); timerSubscription: Subscription; this.timerSubscription = this.timer.subscribe((value) => { this. ...

Tips for transferring several parameters from an Angular 8 application to a .NET Core API

Hello, I am facing an issue with passing multiple string parameters from an Angular 8 app to a .NET Core API. This is my first time posting a question so I will try to provide all the necessary details. Here is the Angular code snippet: import { Injectab ...

Error: The object is not defined (evaluating '_$$_REQUIRE(_dependencyMap[32], "react-native-safe-area-context").SafeAreaView')

I am currently working on developing a chat application using react-native with the following dependencies: "dependencies": { "@react-native-async-storage/async-storage": "~1.17.3", "@react-native-community/masked ...

Using Typescript, pass a Sequelize model as a property in NodeJS

Currently in the midst of a project using Node, Typescript, and Sequelize. I'm working on defining a generic controller that needs to have specific functionality: class Controller { Schema: <Sequelize-Model>; constructor(schema: <Sequel ...