Select dropdown shows blank when NG model is null - html

I am trying to change the second dropdown to default "Select ... " option when i change anything on first dropdown. But i keep showing me blank space, i think the NG is adding extra Option tag in the beginning and i dont know why.
Here is my HTML
<div>
<select (change)="onDropChange($event.target.value)" [(ngModel)]="person.name">
<option value='default' > Select Name</option>
<option *ngFor="let item of list"> {{item}} </option>
</select>
</div>
<select [(ngModel)]="person.options">
<option value='default' [selected]="!person.options"> Select Option</option>
<option *ngFor="let item of options" value="{{item}}"> {{item}} </option>
</select>
Here is my TS
person= {
person: "sam",
options: 123
}
list = ["kenny", "sam", "stan"];
options = [122,4324,234,123]
onDropChange(value){
this.purchaser.options = null
}
code: https://stackblitz.com/edit/angular-3xo9sv?file=src%2Fapp%2Fapp.component.ts

It's because you don't have any option with value = null, wich is the value you're assigning to this.person.options.
Option 1
Make your default option to be equal to null:
<select [(ngModel)]="person.options">
<option [value]="null" [selected]="!person.options"> Select Option</option>
<option *ngFor="let item of options" value="{{item}}"> {{item}} </option>
</select>
note the [value]="null". Now, there's actually an option that matches with the value you're assigning to it.
Option 2
If you don't want your default option to be equal to null, just make sure that the value you're assigning to this.person.options matches with your default option. In the code you provided it's value='default', so basically you just need to
onDropChange(value){
this.person.options = 'default';
}
Make sure giving proper types to your object since right now it's declaring it as number so you may have a problem. You may create your type but an easy/quick fix to get rid of this problem is declaring it as any.
Again, I suggest to define this better.
export class AppComponent {
person:any = {
name: "sam",
options: 123
}
...
}

Related

Store name and value of select option element in different fields in Angular

I am using the below code to create a select field . Here the value is getting updated to the ngmodel . I also want to get the name of the option to store in another field
ie: Value in one field (ID) and name in another field (Selected value) .
Is there any way to achieve this .?
( <select name="ORG_ID" #ORG_ID="ngModel" [(ngModel)]="siteuses.ORG_ID" class="form-control"
[class.is-invalid]="!isValid && (siteuses.ORG_ID==''|| siteuses.ORG_ID==null)">
<option *ngFor="let item of this.sharedService.l_operating_units_s" value="{{item.BU_ID}}">{{item.NAME}}
</option>
</select>
)
you can use a getter
get nameOperating()
{
operating=this.sharedService.l_operating_units_s
.find(x=>x.BU_ID==siteuses.ORG_ID);
return operating?operating.NAME:null;
}
or store the full item selected using [ngValue] in the options
<select name="ORG_ID" #ORG_ID="ngModel" [(ngModel)]="site" class="form-control">
<option *ngFor="let item of this.sharedService.l_operating_units_s"
[ngValue]="item">{{item.NAME}}
</option>
</select>
<!--just for check-->
{{site.NAME}}{{site.BU_ID}}
In "site" you has the full selected, so in site.NAME you has the name, and in site.BU_ID you has the ID

Default select value for dropdown is not straight forward in Angular 7

I've been working with Angular 7 recently, and finding some strange stuff happening when trying to display a dropdown list, and showing a default value within this list. Here are two separate lists respectively:
sample.component.html
<select [(ngModel)]="organization" id="organization" required>
<option *ngFor="let organization of organizations" [value]="organization.id" [ngValue]="organization.name">{{organization.name}}</option>
</select>
<br>
I find that the above code displays the following in my browser:
Notice how the default value is set in the above, but in the next drop down the default value isn't set:
sample.component.html
<select [(ngModel)]="seniorExperience" id="seniorExperience" required>
<option *ngFor="let seniorExperience of seniorExperiences" [value]="seniorExperience.seniorExperienceId" [ngValue]="seniorExperience.seniorExperienceName">{{seniorExperience.seniorExperienceName}}</option>
</select>
The browser displays the following:
Why is the second dropdown not populated with the a default, whilst following the same syntax? From searching and reading docs I presumed that [ngValue] sets the default value of the dropdown, which seems to work for the first dropdown. Is there a better way to set the default select?
As per Angular 7 implementation:
In HTML:
<select *ngIf="ownerLevels" [value]="selectedOwnerLevel" (change)="onChangeOwnerLevel($event.target.value)" formControlName="ownerLevel" id="ddOwnerLevel">
<option [value]="0" disabled>Select Owner Level</option>
<option *ngFor="let ownerLevel of ownerLevels" [value]="ownerLevel.id">{{ownerLevel.name}}</option>
</select>
In TS:
ownerLevels = [
{ id: 1, name: 'Company' },
{ id: 2, name: 'Department' },
{ id: 3, name: 'Personal Area' }
];
selectedOwnerLevel: number = 0;
onChangeOwnerLevel(ownerLevelId: number) {
this.selectedOwnerLevel = ownerLevelId;
}

Select box default selected value not showing in selectbox in angular 2

I'm trying to do a select operation with default value set for my edit page. However, it's not showing in select box
<select name=moduleId [(ngModel)]="moduleName" >
<option value="{{model.moduleId}}" selected>{{moduleName}}</option>
<option *ngFor="let accss of access" value="{{accss.moduleId}}">{{accss.moduleName}}</option>
</select>
showing like this default value is right below in options.
Remove this value="{{model.moduleId}}" to value="{{moduleName}}"
<select name='moduleId' [(ngModel)]="moduleName">
<option value="{{moduleName}}" selected>{{moduleName}}</option>
<option *ngFor="let accss of access" value="{{accss.moduleId}}">{{accss.moduleName}}</option>
</select>
and in component add object as
access = [{
moduleId: 1,
moduleName: 'abc'
}, {
moduleId: 2,
moduleName: 'def'
}];
moduleName = 'Select';
The problem might be value in options change to [value] attribute binding
<select name=moduleId [(ngModel)]="moduleName" >
<option *ngFor="let accss of access" [value]="{{accss.moduleId}}">{{accss.moduleName}}</option> // change to this
</select>
COMPONENT
export class Component {
moduleName: any = <yourselecteddefaut>;
}

How to have a default "please select" option on an Angular select element with a null value for validation?

I have a select HTML element in an Angular ngFor loop:
<select formControlName="type" required>
<option *ngFor="let type of typeList" [ngValue]="type.value">{{ type.caption }}</option>
</select>
In Internet Explorer, the first item in the list is selected when the template loads, but it's value isn't set in the form control, so causes a 'required' validation error until another value is selected in the list. I want to have a 'Please select' option that has a null value to prevent this happening.
This is for Angular 2+ used with or without TypeScript
Add an option like so:
<option [ngValue]="null">Please select</option>
So your control will look like:
<select formControlName="type" required>
<option [ngValue]="null">Please select</option>
<option *ngFor="let type of typeList" [ngValue]="type.value">{{ type.caption }}</option>
</select>
This works as Angular inspects the value attribute because of the square brackets. The value becomes a true null, unlike if we used value="" (this is just an empty string and doesn't match null).
In case you're not using ngForm, another approach to implementing the selected value is to do something like [value]='selectedType' (change)='selectedType = $event.target.value' in your select tag. For example:
In your component.ts:
public selectedType: string;
In your component.html
<select [value]='selectedType' (change)='selectedType = $event.target.value'>
<option value=''>-- Select your Type --</option>
<option *ngFor="let type of typeList" [ngValue]="type.value">{{ type.caption }}</option>
</select>
component.ts
public selectedValue = 'None';
component.html:
<div ">
<label>Highlight</label>
<select [(ngModel)]="selectedTrunk" (change)="onChangeTrunk($event)">
<option value='None'>None</option>
<option *ngFor="let trunklist of DRLNameTrunkList" [selected]="trunk" [value]="trunklist.SIPTRUNKNAME">{{trunklist.SIPTRUNKNAME}}</option>
</select>
</div>
This is my code pelase modify as per your requirements

Set the selected value of the drop down in Angular 2

vatCodeList is an error with string codes.
Example: ['34u' , '23' ,'tt']
Need to set the selected value there .
<select class="custom-select" formControlName="vatCode">
<option *ngFor="let i of vatCodeList">{{i}}</option>
</select>
Inside your *.component.ts
public vatCode: any;
Inside your *.component.ts you can set the value of vatCode to one of the values contained within vatCodeList, this will update the selected value.
Inside the *.component.html
<select class="custom-select" formControlName="vatCode" [(ngModel)]="vatCode">
<option *ngFor="let i of vatCodeList">{{i}}</option>
</select>
You can bind the value property like this
<option [value]="i" *ngFor="let i of vatCodeList">{{ i }}</option>
You can try to put an expression to the option tag to make an option selected
<select class="custom-select" formControlName="vatCode">
<option *ngFor="let i of vatCodeList" {{i == vatCode?'selected':'' }}>{{i}}</option>
</select>
The variable should reference the value of the InputControl. Using reactive forms it would be easy to extract the value and put it into expression.
The easiest way to bind the element to the model with ngModel but you can check if this solution helps.