Unchanging value in md-select even after selection

Having an issue with the md-select component in Angular Material 2. When I change the value of the select, it updates correctly but the displayed value remains the default "LTC" option. I want to display the currently selected option instead of the default. Any assistance is appreciated!

component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-allcoins',
  templateUrl: './allcoins.component.html',
  styleUrls: ['./allcoins.component.scss']
})
export class AllcoinsComponent implements OnInit {

  regTypeSelectedOption: string = "LTC";
  selectedNav: any; 
  navs = ['LTC', 'ETH', 'ZEC', 'XRP']

  constructor() { }

  setNav(nav:any){
    this.selectedNav = nav;
        if(this.selectedNav == "LTC"){
            this.regTypeSelectedOption = "LTC";
        }
        else if(this.selectedNav == "ETH"){
            this.regTypeSelectedOption = "ETH";
        }
        else if(this.selectedNav == "ZEC"){
            this.regTypeSelectedOption = "ZEC";
        }
        else if(this.selectedNav == "XRP"){
          this.regTypeSelectedOption = "XRP";
      }
    }

  ngOnInit() {
    this.selectedNav = 'select value';
  }

}

html

<div class="main-content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header card-header-text">
                        <h4 class="card-title">Buy Cryptocurrencies</h4>
                        <p class="category">Select the cryptocurrency you want to buy</p>
                    </div>
                    <div class="card-content">
                        <md-select placeholder="Currency" [(ngModel)]="regTypeSelectedOption">
                            <md-option [value]="regTypeSelectedOption" (click)="setNav(item)" *ngFor="let item of navs">{{item}}</md-option>
                        </md-select>
                        <app-comprarzec *ngIf="regTypeSelectedOption === 'ZEC'"></app-comprarzec>
                        <app-comprarxrp *ngIf="regTypeSelectedOption === 'XRP'"></app-comprarxrp>
                        <app-comprarltc *ngIf="regTypeSelectedOption === 'LTC'"></app-comprarltc>
                        <app-comprareth *ngIf="regTypeSelectedOption === 'ETH'"></app-comprareth>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Answer №1

To handle this situation, utilize the change event (change)="". This way, you can manage each time a new option is selected and trigger the corresponding event.

Regarding your issue, all values are set to [value]="regTypeSelectedOption", with "LTC" as the default value. You only need to use regTypeSelectedOption in NgModel, not within the value attribute.

UPDATE

In your scenario (where the value of md-options matches the item):

<md-option [value]="item" (click)="setNav(item)" *ngFor="let item of navs">{{item}}</md-option>

Give it a try and see if it resolves the issue.

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

Error: The JSON.parse method encountered an unexpected token 'h' at the beginning of the JSON string while trying to parse it

Here is the code snippet I wrote using Ionic framework: dialogflow(question) { let headers = new Headers(); headers.append('Content-Type','application/json'); return this.http.post('http://localhost:3000/api/dialogflow ...

Ways to transfer specific properties from one object to another in TypeScript

I'm currently working on a function that selectively copies key-value pairs from one object to another in order to remove certain properties. The code snippet for this function is shown below: sanitizeData: function (sourceObject: object, ...allowedKe ...

When running ng new command, an error may occur with dryRunSink.(...).concat not functioning properly

I've been attempting to create a new Angular project by following the steps outlined on this website: https://github.com/angular/angular-cli However, each time I try to initiate a new project using the ng new command, an error occurs. E:\Code&b ...

What is the best way to send email using angular from server/cPanel?

Currently, I am working on an Angular application that includes a contact form. Once a user submits the form, I would like to automate the process of sending an email to myself with the information provided by the user. Can I send the email directly from ...

Exploring the functionality of the 'or' option in typescript-sequelize when working with node.js

I am currently utilizing typescript-sequelize I need guidance on how to properly implement the findOne({where: {something or something2}}); method. I have tried reviewing the documentation, but I am struggling to comprehend it. Can someone provide assista ...

Currently utilizing Angular 9 with the Ivy compiler, my goal is to showcase a PDF file in a preview format. Simple solution being the ngx-doc-viewer, however encountering errors during implementation

Errors detected in ngx-doc-viewer module: "CommonModule" export cannot be imported from non-EcmaScript module "Component" export cannot be imported from non-EcmaScript module "DomSanitizer" export cannot be imported from non-EcmaScript module "EventEmit ...

What is the best way to highlight rows for expansion in a primeng table?

Currently, I am experimenting with primeng table row expansion by following this specific example: https://stackblitz.com/edit/github-7gxyuy?file=src/app/app.component.html Despite my efforts to have each expansion row selected individually, I encountered ...

When TypeScript in IntelliJ fails to generate JavaScript files after enabling the tsconfig declaration

In my tsconfig file, I have the following setup: { "compilerOptions": { "module": "ESNext", "target": "es6", "sourceMap": true, "rootDir": "./&qu ...

What is the best way to search a list with a button click using Angular?

I'm currently working with Angular 6 to create a search functionality for my list. The current code I have successfully performs the search when I type text into the search input box. However, I want the search to only be executed after I click on a b ...

Setting attributes for elements in NativeScript/Angular can be accomplished by utilizing item properties

Is it possible to call a function on an item element inside an ngFor loop in order to set some properties? Otherwise, I find myself having to loop twice - once in the script and then again in the template - setting temporary properties to a model that shou ...

Sample test scenario for a service function that includes an HTTP subscription using Angular's HTTP RxJS

I have a service method that includes an HTTP call and subscribes immediately to the response code in order to execute the rest of the action command based on it. Here is an example of the service method: processData(id): void { const url = `http://l ...

Establishing global date restrictions for the DatePicker component in Angular 8 using TypeScript across the entire application

I am currently learning Angular 8 and I am looking to globally set the minimum and maximum dates for a datepicker in my application. I would like to accomplish this by using format-datepicker.ts. Any suggestions on how I can achieve this? Min date: Jan 1, ...

What are some methods for simulating user interaction on input and button elements?

Here is a code snippet available in this stackblitz demo. Essentially, there is a basic form with an input field and a button. Clicking the button will copy the current value of the input to a label: https://i.stack.imgur.com/pw3en.png after click: htt ...

Tips on retrieving enum values in typescript

Having trouble retrieving values from an enum? Check out this snippet of code: export const enum ComplianceType { ENGINEER_ASSESMENT = 'ENGINEER_ASSESMENT', CONSTRUCTION_COMPLIANCE = 'CONSTRUCTION_COMPLIANCE', ARCHITECTURE_ASSIGN ...

What is the best method for storing client data in Angular to be utilized in a REST API for retrieving information?

When a user logs into my Angular application, I retrieve their data and a JSON Web Token from the server via a REST API request and store it in local storage. Subsequently, I utilize the user's data, including their email, account status, and ID, in ...

Diving deeper: Angular and NPM dependencies compared to devDependencies

After doing a lot of reading on this topic and following this very informative post at: What is the distinction between dependencies, devDependencies, and peerDependencies in an npm package.json file? I have learned that dependencies should include all ru ...

Tips on modifying the selected type key name through Pick?

I currently have this structure: type Product = { name: string } and I am looking to extract the name property and use it in a different type declaration like so: type NewProduct = Pick<Product, 'name'> Now, I want to rename name as new ...

Using import statement is mandatory when loading ES Module in TS Node: server/src/index.ts

Attempting to kickstart a TypeScript Node project, I've gone ahead and added some essential dependencies (TypeScript, ESLint, Mongoose, and GraphQL). However, upon executing the command: ts-node-dev --respawn --transpile-only src/index.ts An error me ...

Ensure that the dropdown remains open at all times when using the angular2-multiselect-dropdown package

I am currently implementing the angular2-multiselect-dropdown in my Angular application. This dropdown is being used within a popup. What I am trying to achieve is that when a button is clicked, the popup should display the dropdown in an open mode witho ...

How to Create an Effective Navigation Bar in React?

I am facing an issue while trying to develop an app with a NavBar in React. Despite everything looking correct in my browser, I am unable to render a new element under the nav-bar after clicking the NavLink. https://i.sstatic.net/g0jJv.png App.ts Main co ...