Exploring the process of dynamically updating a form based on user-selected options

I need assistance with loading an array of saved templates to be used as options in an ion-select. When an option is chosen, the form should automatically update based on the selected template.

Below is the structure of my templates:

    export interface Template {
 ...
  destination: string; //iban
  recipient: string;
  amount: number;
  reference: string;
}

Here is the code snippet for my ion-select component:

 <ion-item>
           <ion-label>Load template</ion-label>
           <ion-select (change)="this.transactionForm.patchValue({recipient: template.recipient, destination: template.destination, amount: template.amount, reference: template.reference})">
             <ion-option *ngFor = "let template of templates;">
               {{template.reference}}
             </ion-option>
           </ion-select>
         </ion-item>

The goal is to populate the select menu with saved templates and dynamically update the form fields upon selection.

This is how I set up the form in the constructor of the .ts file:

 constructor( public formBuilder: FormBuilder, public templateServicerino: TemplateService) {
    this.templateServicerino.createTemplate("DE365849", "John Johnson", 420, "Testerino");
    this.templates = this.templateServicerino.getAllTemplates();
    this.transactionForm = this.formBuilder.group({
          recipient: [''],
          destination: [''],
          amount: ['0'],
          reference:  ['']
        });

However, when testing, I encounter an issue where the form does not get updated after selecting a template. My IDE reports that the field "template" is unresolved.

Your assistance is greatly appreciated. Thank you!

Answer №1

Is there an issue with accessing the template reference outside the element scope? The change method being used is not specific to Ionic, so please refer to this link for more information: https://github.com/ionic-team/ionic/issues/7807

For further details, you can also check the Ionic documentation here: https://ionicframework.com/docs/api/components/select/Select/

<ion-item>
      <ion-label>Load template</ion-label>
      <ion-select  [(ngModel)]="selectObj" (ionChange)="onSelectChange($event,selectObj)" 
       >
      <ion-option *ngFor = "let template of templates;">
       {{template.reference}}
      </ion-option>
     </ion-select>
    </ion-item>

Remember to add this code in the component after the constructor method.

 onSelectChange(selected:any,selectObj){
  console.log(selected,selectObj)
  this.transactionForm.patchValue({recipient: selectObj.recipient, destination: 
  selectObj.destination, amount: selectObj.amount, reference: selectObj.reference})
 }

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

NextJS is throwing an error stating that the element type is invalid. It was expecting either a string for built-in components or a class/function for composite components, but instead received an object

I encountered the following issue: Error - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but received an object. Here's the code from my \components\LayoutWrapper.js: i ...

Troubleshooting a React Node.js Issue Related to API Integration

Recently, I started working on NodeJs and managed to create multiple APIs for my application. Everything was running smoothly until I encountered a strange issue - a new API that I added in the same file as the others is being called twice when accessed fr ...

Ways to release a client-side script with npm?

Within my nodejs package, I have included code that can be executed on both the backend and in a single .js file for browsers. In order to utilize the browser script, it must be placed within a script element in an HTML file. My query pertains to whether t ...

I am interested in utilizing the request-reply pattern with KafkaJS in a TypeScript environment. Could you provide guidance on how to successfully implement this?

I am new to Kafka and I am trying to implement the request-reply pattern using kafkajs in TypeScript. However, my current implementation is very basic and the consumers inside producers are taking too long to start, causing delays. Is there a better way to ...

Failure to trigger Summernote's OnImageUpload function

After transitioning to the latest version of Summernote, which is Version 7, I encountered a problem with the image upload functionality. Despite specifying the line onImageUpload: function(files) {sendFile(files[0]);}, it seems that this code is not being ...

Sending Angular 4 POST request to Java Spring Controller via HTTP

Hey there, I'm looking to pass a string from my Angular 4 post request to my Java Spring MVC controller and get its value returned. In the Angular 4 function: let body = 'example' http .post('favourite', body) .subscribe( ...

The condition in a Typescript function that checks for strings will never evaluate to true

I encountered a strange issue with a TypeScript condition in a function. Here is my current code, where the parameters are passed from outside: getLevel(validation: string, status: string): string { let card = ""; if (validation == &qu ...

Monitor changes in the visible price range in lightweight-chart

Is there a way to detect when the visible price range changes in lightweight-chart, similar to how the timeScale's visible time range change can be detected through subscribeVisibleTimeRangeChange? I couldn't find anything related in the document ...

Angular: A module in the library declares an HttpInterceptor that intercepts requests that are made outside of the

I am encountering an issue with Angular where a custom instance of HttpInterceptors within an Angular library is intercepting requests for HttpClient calls made outside the library (i.e. in the consuming application). I'm finding it difficult to gras ...

JavaScript / Regular Expression: remove the initial <p> tag if it meets a specific condition

Whenever I receive HTML content from the API, it may come in different formats. Sometimes, it looks like this: <p>::type/12</p> <p>Some content</p> <p>Some more content</p> Other times, it might not have the first para ...

The element in Selenium's net.serenity.bdd.core.exceptions.SerenityManagedException has encountered a timeout issue

I'm having difficulty choosing a radio button on this particular form: <form _ngcontent-c4="" novalidate="" class="ng-untouched ng-pristine ng-invalid"> <div _ngcontent-c4="" class="text-center"> <div _ngcontent-c4="" class=" ...

Prevent scrolling in AngularJS model popups

When loading data for the first time in a model popup, the scroll bar is not displayed inside the popup. However, after performing a search function and filtering the data, the scroll bar appears inside the model popup. How can this issue be fixed? this ...

Update a document by removing elements from an array where the objects are of a certain value

I am trying to remove specific tweet objects from the timeline list within the user model. The tweets I want to remove are those authored by a user with a matching id user._id. I attempted the following approach: router.get("/follow/:userId", is ...

What is preventing Ionic from locating my model interface?

I recently started working with Ionic and I am using a tutorial as a guide to integrate Firebase authentication into my project. However, I am encountering an issue with the user interface that is being generated. When I run ionic serve for the first time ...

What is the process of directing to another HTML page within the same Express controller script?

I am looking to switch the initial page (login page) to a second page (admin dashboard) from within the same controller in Express after a specific action has been taken. Here is the relevant code snippet from my controller file nimda.js: function handle ...

Adding a custom role in Angular TypeScript in Microsoft AppInsights is a straightforward process that can provide valuable

I have an angular project where I am looking to incorporate AppInsight with custom telemetry (role). The project is built in Angular using TypeScript, and I successfully integrated appinsights by following this tutorial. However, when attempting to add cus ...

Perform an Ajax call just one time

$('#addToCart').click(function () { let csrf = $("input[name=csrfmiddlewaretoken]").val(); let trTable = $(this).parents('div')[1]; let customPrice = $($(trTable).children('div') ...

Example of Node-gallery used in isolation, displaying an error event with the message "ENOENT"

I am currently experiencing an issue with the node-gallery npm module. After installing dependencies inside the /example directory, I attempted to run the app. The result was a localhost:3000/gallery page, but upon the page fully loading, I encountered the ...

Utilizing JavaScript and jQuery libraries in conjunction with periods

I am a bit puzzled about when to include the period before referencing class names. For instance, in this code snippet, why is a period included before the first use of the 'active-slide' class but not for the other two instances? var primary = ...

Spontaneously generating models

Explaining this concept can be a bit complex. I am tasked with creating an object that contains properties from dynamic HTML code. To illustrate, let's use an example: Firstly, here is my data object: var myObject = {Field1: 'Value1', Fiel ...