"Learn how to dynamically update a value based on the selected option in a drop-down menu in Angular

Hello everyone. I am working on an angular project that includes a product page. Some products on this page can have multiple types, like so:

Product ID-1 Type ID-1 Price-$10

Product ID-1 Type ID-2 Price-$15

Product ID-1 Type ID-3 Price-$25

In this scenario, I need to display the types in a dropdown menu. When a type is selected, I want to update the price accordingly. How can I retrieve the selected Type ID and Price in order to send it to the web API from my component.ts file?

Using Angular-7 CLI.

Product.component.html

<h5 class="product-price">**I WANT TO SHOW THE SELECTED DROP-DOWN VALUE PRICE HERE**</h5>

<h5>Select Type</h5>
<select
   [(ngModel)]="selectedType" > 
   <option 
       *ngFor="let type of productType" 
       [ngValue]="type.id">
        {{type.name}}
   </option>
 </select> 

Product.component.ts

 selectedType: { id: any; name:any ; price:any };

 productType:Array<Object> = [
  {id: 1, name: "100 ml", price:2000},
  {id: 2, name: "200 ml", price:4000},
  {id: 3, name: "300 ml", price:3000}
 ];

Answer №1

To retrieve the price from the model variable, you can set the object as the value of the ngValue property.

<h5 class="product-price">{{selectedType?.price}}</h5>

<h5>Select Type</h5>
<select
   [(ngModel)]="selectedType" > 
   <option 
       *ngFor="let type of productType" 
       [ngValue]="type">
        {{type.name}}
   </option>
 </select> 

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 InAppBrowser seems to have trouble loading pages with cookies when I attempt to navigate back using the hardware back button

In my Ionic/Angular application, I utilize the InAppBrowser plugin to access a web application for my company. The InAppBrowser generally functions properly; however, there is an issue with a cookie within the web application that controls filters for cert ...

Mastering the art of throwing and managing custom errors from the server to the client side within Next.js

I'm in the process of developing a Next.js application and I am faced with the challenge of transmitting customized error messages from the server to the client side while utilizing Next JS new server-side actions. Although my server-side code is func ...

The specified property is not present on the given type

I am receiving data from an API, and I have defined its structure like this interface DailyData { dt: number; sunrise: number; sunset: number; moonrise: number; moonset: number; moon_phase: number; temp: {day: number, eve: number, max: number ...

Discovering class methods in typescript

Currently, I am running TypeScript unit tests using Mocha Chai after configuring the compiler options to ts-node. Within one of my unit tests, I am seeking a way to retrieve all methods from a utility class that I have designed and execute the same set of ...

Altering the variable name causes the code to malfunction

Here is the code snippet I am using to execute an angular application from a node.js server: const root = path.join(__dirname, 'frontend/dist', 'learn-playV2'); app.get('*', function (req, res) { fs.stat(root + req.path, fu ...

The argument provided is a string type, which cannot be assigned to a parameter expecting an object with a 'results' property of type string

When attempting to pass the result.nativeEvent.message to another function, I am encountering the error: Argument of type 'string' is not assignable to parameter of type '{ results: string; } on onUnityMessageController(result.nativeEvent.me ...

Testing TaskEither from fp-ts using jest: A comprehensive guide

Entering the world of fp-ts, I encounter a function (path: string) => TaskEither<Erorr, T> that reads and parses configuration data. Now, my challenge is to create a test for this process. Here is what I have tried so far: test('Read config& ...

Error TS5083: Unable to locate the file node_modules/gts/tsconfig-google.json while setting up Angular CLI on MacOS

I encountered issues while trying to install Angular CLI on my Mac using sudo npm install -g @angular/cli. The latest error message I received is as follows: npm WARN deprecated @npmcli/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cf ...

The Angular Behaviour Subject is failing to accurately represent the change status within the component

I am currently working on a MEAN project that involves an Angular Behaviour Subject emitting the status of the logged-in user. Issue: After the user logs in, the app component detects the change in user status. However, the header component (which is part ...

"Troubleshooting Console Errors in NextJS with TypeScript Integration and Fluent UI Components

I am currently working with NextJS, TypeScript, and Fluent UI. To set up the app, I used the command yarn create next-app --typescript. Afterwards, I incorporated Fluent UI into my project by running $ yarn add @fluentui/react. So far, I have not made an ...

Encountering unusual results while utilizing interfaces with overloaded arguments

I came across a situation where TypeScript allows calling a method with the wrong type of argument. Why doesn't the TypeScript compiler flag this as an issue? interface IValue { add(value: IValue): IValue; } class NumberValue implements IValue { ...

Having an issue in Angular 2 where the correct function is not triggered when a button is placed within a selectable anchor

Within an anchor element, I have a button that triggers its own click listener for an editing popup module. The anchor itself has another click listener assigned to it. For example: <a (click)="onClick(myId)" class="list-group-item clearfix"> < ...

Error in Typescript Observable when using .startWith([])

I encountered the TypeScript error below: Error:(34, 20) TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'number | Scheduler'. Type 'undefined[]' is not assignable to type 'Scheduler& ...

Build a custom Angular2 pipe to convert JSON data into an array through iteration

I am attempting to take the JSON data provided below and convert it into an array for use with *ngFor='let item of items', which will allow me to display data as item.name, etc... This is what I have tried: var out = []; for(var key1 in object) ...

When using Vue.js, you may encounter an error message stating that the `document.title` type of 'undefined' cannot be assigned to type 'string' in Typescript

I'm trying to use beforeEnter with Vue Router, but I encountered an error: TS2322: Type 'string | symbol | null | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string&apo ...

What could be the reason for a property going unnoticed during the iteration of a list?

The Scenario There is a class named myClass: export class myClass { name: string; age: number; city: string; } and another class called people: export class people { name: string; age: number; } In the component.ts, a variable list ...

Successful submission has been made without encountering any 'Access-Control-Allow-Origin' console error

My attempt to submit the HTML Form was done in various ways. Using a single demo HTML File - Successful submission (No errors and HTTP 200 response code) Submission via Chrome POSTMAN plugin - Success (No errors and HTTP 200 response code) However, enco ...

Angular: Autocomplete field automatically shifts focus when an item is removed

I am currently working on an Angular 2 application that utilizes PrimeNG components. One of the UI features includes an autocomplete component with multi-select functionality (p-autoComplete) similar to the one showcased in the official documentation: < ...

The animation fails to retain its final state and reverts back to its initial state

Currently, I am diving into learning Angular 6 and encountered a small issue. Following this tutorial: Upon clicking the button, the animation executes as intended. However, after the fade-out effect, the text reappears abruptly. Any insights on why it re ...

Encountering an issue with compiling Angular due to a Type Inference error

interface Course { name: string; lessonCount: number; } interface Named { name: string; } let named: Named = { name: 'Placeholder Name' }; let course: Course = { name: 'Developing Apps with Angular', lessonCount: 15 }; named = ...