What is the best way to retrieve specific values from an ng-bootstrap modal?

I am working on a website that includes an ng-bootstrap modal. This modal contains two <select> elements with dynamic items, allowing the user to click on two buttons. One button simply closes the modal and sets the value of the Reactive Form to new Date(). However, when the other button is pressed, I need to retrieve the values from both <select> elements and set them as the value of the Reactive Form.

Since I am new to angular, I am unsure how to extract the values from the <select> elements upon clicking a button. Can anyone provide guidance on this?

Below is the code snippet for my Modal:

<ng-template #programma let-modal>
  <div class="modal-header">
    <h4 class="modal-title" id="modal-basic-title">Choose a time</h4>
    <button
      type="button"
      class="close"
      aria-label="Close"
      (click)="quando(true, modal)"
    >
      <span aria-hidden="true"&g;t&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <select class="btn btn-lg btn-block btn-outline-primary" >
      <option *ngFor="let giorno of giorni" [value]="giorno.time">{{ giorno.formatted }}</option>
    </select>
    <select class="btn btn-lg btn-block btn-outline-primary mt-3">
      <option *ngFor="let ora of ore" [value]="ora">{{ ora }}</option>
    </select>
    <button
      type="button"
      class="btn btn-lg btn-block btn-primary"
      (click)="quando(false, modal)"
    >
      Schedule
    </button>
    <button
      type="button"
      class="btn btn-lg btn-block btn-dark"
      (click)="quando(true, modal)"
    >
      Deliver now
    </button>
  </div>
</ng-template>

Here is the quando() method used to set the value in the Reactive Form:

quando(val: boolean, modal?: any): void {
    this.quandoCheck = val;
    if (val) {
      this.checkoutForm.get('spedizione.quando').patchValue(new Date());
    } else {
      // Need to get values from SELECT and set them to this.checkoutForm.get('spedizione.quando')
    }

    if (modal){
      modal.close();
    }
  }

Answer №1

How can I retrieve the values from a select input?

There are two ways to bind a model to select inputs:

  1. Reactive (using formControl):
  • In the template:
<select [formControl] = "select1Control" ...
  • In the controller:
@Component({...})
export class MyComp {
select1Control = new FormControl('');
...
  • How to access the value:
console.log(this.select1Control.value)
  1. Template driven (using ngModel):
  • In the template:
<select [(ngModel)] = "select1" ...
  • In the controller:
@Component({...})
export class MyComp {
select1 = ''
...
  • How to access the value:
console.log(this.select1)

Try it out on Stackblitz

Other options: Using form groups / using references.

Note: Make sure to import form (or reactiveForm) modules.

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 most effective method for determining if an object contains a particular property?

Can anyone advise on the best approach to check if an ajax response object has a specific property? I've done some research and it seems there are various methods to do this. One way is: if(ajaxResponse.hasOwnProperty('someProperty')){ ...

How can I resolve the error when Model.findOne() does not support a callback anymore?

I am facing an issue with this code as it is returning the error "Model.findOne() no longer accepts a callback". I need to resolve this issue without downgrading my mongoose version. `router.post('/login', async(req, res) => { const email = r ...

Passing events from Vue to child components

Looking for a way to make clicking on the outer pill element have the same effect as clicking on the checkbox itself? Check out this HTML code that renders these little boxes: https://i.sstatic.net/hspnF.png <div class="token-checkboxes"> <spa ...

What is the process for invoking a method that accepts a List<string> type parameter through an ajax call

I have been trying to figure out how to make an AJAX call to a C# method from jQuery, but it seems that the code below is unable to send the value to the "SessionTemplate(List list)" method. How can I successfully pass a value of type List? [HttpPost] pub ...

The parameters 'event' and 'event' are not compatible with each other

I'm currently working on a page that involves submitting a form: import React from 'react'; import Form from 'react-bootstrap/Form'; import { useSignIn } from '../../hooks/Auth/useSignIn'; import { useHistory } from &apos ...

Tips for showing nested array values in Angular 8

I'm new to using angular and I'm attempting to display values from an array within another array in a table format. Here is my JSON array that I'd like to display in rows: { "data": { "Influencer": [ { ...

Make window.print() trigger the download of a PDF file

Recently, I added a new feature to my website allowing users to download the site's content by following these instructions: Printing with Javascript || Mozilla Dev. Essentially, the user can print a newly formatted site stored in a hidden iframe. Now ...

Node.js readline: SyntaxError: Unexpected token =>

Currently, I am diving into node.js and have found myself in need of utilizing the readline module for a new project. Below is the code snippet that I extracted directly from the official readline module example. const readline = require('readline&ap ...

Tips for fixing the issue: Absence of the environment variable 'TWITTER_CONSUMER_KEY'

Being a novice in programming, I didn't anticipate encountering errors so soon. Today, I attempted to code a Node.js application for posting Twitter Feeds to Discord but faced failure. I'm puzzled as to why this error is occurring even though I& ...

What is the best way to reduce the size of images for uploading to a website

Our website, developed in .NET with a SQL Server database, functions as a marketplace where users can upload images and product descriptions. However, some customers encounter issues when trying to upload images, particularly when uploading multiple phot ...

New to React and looking for guidance on implementing .forEach or .includes in my code

My task involves going through an array and checking if a string that the user inputs into the form exists in it. This can be achieved using forEach or .includes method. I am basically trying to filter out the array based on the input. If someone could de ...

Managing the dropdown selection will reset the text field when the onchange event is triggered with the submission of the form

My form has multiple sections, and whenever I choose an answer from the dropdown in one section, it clears the data entered in another section. I am using the "onchange" event to display a calculation at the bottom of the form before submission. Section B ...

The Angular Cli seems to be having trouble loading a State property and its reducer within the ngrx store all of

Following some minor changes to my application, I encountered an issue with my ngrx store not loading properly. While most of the store properties are displaying as expected and even fetching API results through their reducers, I am noticing that a crucial ...

Exploring the World of ES6 Modules, VueJS, and TypeScript

In my pursuit of creating a simplistic setup, I aim to incorporate the features mentioned above. In the realm of JavaScript code, below snippet plays a crucial role: <script type="module" src="./myapp.js"></script> This script is responsible ...

Protractor Error: Identifier Unexpectedly Not Found

I've encountered a slight issue with importing and exporting files while working on Protractor tests. HomePage.js export default class HomePage { constructor() { this.path = 'http://automationpractice.com/index.php'; this.searchQ ...

Using JSON to bind values to an array of checkboxes

I am working with an array of checkboxes that are populated from a collection. Here is the code snippet: <div class="form-group"> Select days in a week : <td class="even" *ngFor="let item of dayList"> <input value="{{item.check ...

Error in Angular 7 Reactive Forms: "No value accessor available for form control without a specified name attribute"

I'm encountering issues with some form fields while using reactive forms in Angular. Any insights on why this might be happening would be greatly appreciated. Just started working with Angular and Material 7, if that's relevant. An interesting ...

Is there a way to hold off executing a JavaScript script until an XHR request is complete?

A script has been created for sending out a weekly newsletter using PHP. The PHP code is functioning perfectly, but there is also a script in place to keep track of how many newsletters have been sent to ensure all have been sent out. Here is the code: f ...

The ui.bootstrap.carousel component seems to be missing from the display

My Carousel is not displaying for some unknown reason. I have customized the implementation based on my project requirements which differ slightly from the standard guidelines. Despite this, it should function correctly as detailed below. By clicking on m ...

Prevent jQuery from scrolling once the fixed image reaches the bottom of the page

I am struggling to figure out how to keep an image fixed at the top of the footer div while scrolling to the bottom of a webpage. My approach involved calculating the height of the footer in order to adjust the scrolling behavior by subtracting it from th ...