How can I set the default bindLabel for a dropdown in @ng-select/ng-select when the self change event occurs in Angular

I have a scenario where I need to set the default value to null in the ng-select. If the user selects an option from the dropdown first, then on the change event it should check if the Amount model is not null or blank. If the Amount model is blank, then the ng-select should be set to null.

In the HTML component:

<input type="text" [(ngModel)]="Amount"/><br/><br/>
<label>Your ng-select</label><br/>
<ng-select [items]="currencyList"
           bindLabel="name"
           bindValue="name"
           placeholder="Select Currency"
           [(ngModel)]="selectedCurrency"
           (change)="onChangeDropdown()">
</ng-select><br/>

In the typescript file for the onchange event function:

onChangeDropdown(){
  if(this.Amount){

  } else {
    this.selectedCurrency = null;
    alert("Please enter an amount first");
  }
}

Here is the link to the dropdown example: Dropdown Example

I also tested with a normal HTML select dropdown and encountered the same issue as mentioned above.

<select [(ngModel)]="selectedCurrency" class="form-control" (change)="onChangeDropdown()">
    <option>--Select Currency--</option>
    <option *ngFor="let c of currencyList" value="{{c.id}}">{{c.name}}</option>
</select>

It only works for the first time. When I try to select more than once, it doesn't work properly. Why does it still show INR or another currency name in the dropdown list?

Answer №1

Set the value of this.selectedCurrency to undefined or an empty string

onChangeDropdown(){
  if(this.Amount){
  
  }else{
    this.selectedCurrency = {};
    // alternatively, set this.selectedCurrency to an empty string
    alert("Please enter an amount first");
  }
}

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

Associating function parameters with object types in TypeScript

In the conclusion of this post, I provide operational code for associating object types with a function that accepts an object containing matching properties. The code snippet I shared results in 'result' being resolved as: type result = { GE ...

Transitioning from Angular Http to HttpClient: Overcoming Conversion Challenges

Currently, I am in the process of converting my old Angular app from Http to HttpClient. While working on the service.ts section, I encountered an error that I am struggling to resolve: ERROR Error: Cannot find a differ supporting object '[object Ob ...

Tips for bypassing the 'server-only' restrictions when executing commands from the command line

I have a NextJS application with a specific library that I want to ensure is only imported on the server side and not on the client side. To achieve this, I use import 'server-only'. However, I also need to use this file for a local script. The i ...

The variable 'form' has not been assigned an initial value in the constructor of the property

Below is the snippet from my component.ts file: import { Component, OnInit } from '@angular/core'; import { Validators, FormControl, FormGroup, FormBuilder } from '@angular/forms'; @Component({ selector: 'app-license', te ...

Angular: Changing the class of a parent element dynamically while iterating through a list with ngFor

I have a unique challenge where I am trying to style a checkbox as a button by placing it inside its label. <label> <input type="checkbox" name="..."> </label> My goal is to toggle the parent label's class based on whether the che ...

Implementing Default Language in Next.js 14 for Static Export without URL Prefix: A Step-by-Step Guide

Currently, I am in the process of developing a website using Next.js 14, with the intention of exporting it as a static site for distribution through a CDN (Cloudflare Pages). The website I am working on requires support for internationalization (i18n) to ...

Tips for enlarging a mat-expansion-panel by pressing a button in Angular?

Currently, I have a component featuring expansion panels. When clicking on the "All" tab button, all mat-expansion-panels expand perfectly in the body below. However, my goal is to make it so that clicking on the B tab will only activate and expand the pan ...

What is the best way to declare a TypeScript type with a repetitive structure?

My data type is structured in the following format: type Location=`${number},${number};${number},${number};...` I am wondering if there is a utility type similar to Repeat<T> that can simplify this for me. For example, could I achieve the same resul ...

The border of the Material UI Toggle Button is not appearing

There seems to be an issue with the left border not appearing in the toggle bar below that I created using MuiToggleButton. Any idea what could be causing this? Thank you in advance. view image here view image here Just a note: it works correctly in the ...

The Ins and Outs of Selecting the Correct Module to Attach a Controller in NestJS CLI

My experience with NestJS has been great so far, especially the Module system and how easy it is to parse requests. However, I have a question about the NestJS CLI. Let's say I have multiple modules. When I create a controller using the command "nes ...

Angular2 material dropdown menu

Currently, I am studying angular2 with its material design. One of the modules I am using is md-select for material and here is a snippet of my code: <md-select> <md-option value="1">1</md-option> <md-option value="2">2< ...

Disguising the Navigation Bar when choosing from various databases

I am currently facing the following issue: <div class="container"> <h3 class="d-flex justify-content-center">Database</h3> <div class="row"> <div class="col-xs-12"> < ...

Embedding a transpiled .js file in HTML using ExpressJS as a static resource

ExpressJS setup to serve transpiled TypeScript files is giving me trouble. Whenever I try to access /components/foo.js, I keep getting a 404 error. /* /dist/server.js: */ var express = require('express'); var app = express(); var path = requir ...

Issue with rendering object in Three.js ply loader

Just starting out with three.js and Angular 13, using three.js v0.137.0. I'm attempting to load and preview a ply file from a data URL, but all I see after rendering is a bunch of lines, as shown in this screenshot - how the ply file renders. The .pl ...

What steps should I follow to utilize a JavaScript dependency following an NPM installation?

After successfully installing Fuse.js using npm, I am having trouble using the dependency in my JavaScript code. The website instructions suggest adding the following code to make it work: var books = [{ 'ISBN': 'A', 'title&ap ...

Is there a way to selectively filter and display certain data in an Angular data table?

I am currently working on a project using Angular 7 frameworks that involves dealing with large amounts of data. One of the tasks is to filter out trial units based on the 'userName' field in the raw data. I have various usernames such as user22 ...

The 'ref' attribute is not found within the 'IntrinsicAttributes' type

I'm currently working on a TypeScript project using React. Although the code is functional, I keep encountering compiler errors with my ref. Here's an example of the code: Firstly, there's a higher-order component that handles errors: expor ...

Sorting arrays of objects with multiple properties in Typescript

When it comes to sorting an array with objects that have multiple properties, it can sometimes get tricky. I have objects with a 'name' string and a 'mandatory' boolean. My goal is to first sort the objects based on age, then by name. ...

Having trouble with JavaScript's Date.getUTCMilliSeconds() function?

I have a straightforward question for you. Take a look at this Angular App and try to create a new date, then print the number of UTC milliseconds of that date in the console. Can you figure out why it is returning zero? ...

A Guide to Implementing Inner CSS in Angular

I am working with an object named "Content" that has two properties: Content:{ html:string; css:string } My task is to render a div based on this object. I can easily render the html using the following code: <div [innnerHtml]="Content.html"& ...