52521

Set default dropdown bindLabel in @ng-select/ng-select on self change event in Angular?

Question:

I have one scenario where i want to set default value to null in <strong>ng-select</strong>. if user select dropdown first then on change event should check Amount model is not null or blank afterward if Amount model is blank then ng-select should be set to null

In 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 typescript on change event function

onChangeDropdown(){ if(this.Amount){ }else{ this.selectedCurrency = null; alert("enter amount first"); } }

here i have attach link : <a href="https://stackblitz.com/edit/ng-select-fbpmsl?file=app/app.component.ts" rel="nofollow">link for dropdown example</a>

Even i also tested in normal html select dropdown this also show me as same output 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's only working for the first time, when I select more then once it's not working. Why this showing still INR or other currency name on dropdown list?

Answer1:

Change this.selectedCurrency value to undefined or empty string

onChangeDropdown(){ if(this.Amount){ }else{ this.selectedCurrency = {}; // or this.selectedCurrency = ''; alert("enter amount first"); } }

Recommend

  • Cannot Start a Session with Class
  • Qt QSqlQuery bindValue works with ? but not with :placeholders
  • PHP PDO Prepared Statements and Value Binding Gives Invalid Parameter Number Error
  • versatile insertion method for PDO class
  • Insert into one Table, while updating another & File Upload
  • Hibernate only loads one object from a list
  • PHP PDO selecting columns with single quotes in their name
  • Octal literals are not available when targeting ECMAScript 5 and higher
  • QSqlQuery how to get value of a count?
  • Compiling Element Causes Input Caret Position to Move to End
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Array with custom indexes in Ionic2
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Typescript - Unable to get 'import' statement to function
  • FFmpeg Conversion Error
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Updating server-side rendering client-side
  • MySQL WHERE-condition in procedure ignored
  • what is the difference between the asp.net mvc application and asp.net web application
  • Web-crawler for facebook in python
  • Matrix multiplication with MKL
  • How to get icons for entities from eclipse?
  • trying to dynamically update Highchart column chart but series undefined
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Trying to get generic when generic is not available
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal