Angular: selected directive on input not working - html

I have the following select in my template:
<select class="form-control" formControlName="tipo" name="tipo">
<option value="" selected disabled>(Select an option)</option>
<option value="FACT">Factura</option>
<option value="NOCRE">Nota Crédito</option>
</select>
however, despite I have put the selected directive to the first one, it does not show if the user does not click the select. It behaves like another option tag.
How can I make this option visible when the component loads without user interaction?

Since value is "" for 'Select..' option, set value "" in it's formcontrol
Working Demo
Try like this:
this.yourForm= new FormGroup({
'tipo':new FormControl(''),
});

why not Adding a model :
in your class :
tipo: string = "";
in HTML :
<select [(ngModel)]="tipo" class="form-control">
<option value="" selected disabled>(Select an option)</option>
<option value="FACT">Factura</option>
<option value="NOCRE">Nota Crédito</option>
</select>

Related

How to add placeholder in the select tag in angular Typescript?

I'm trying to use a placeholder for dropdowns in the select tag, but the placeholder isn't working. Also, I tried some other options, but they didn't work.
<select class="form-control" [(ngModel)]="Category" required placeholder="Category">
<option value="" disabled selected>Select one category </option>
<option *ngFor="let data of array" [value]="data.Value">{{data.Name}}</option>
</select>
I have a select tag where I remove the [(ngModel)]="Category" properties and it shows the placeholder option, but when I add them it shows Blank in Placeholder.
In TS File declare your [(ngModel)] as null, it will work,
I have attached the stackblitz demo as well.
You can use an option with disabled.
In TS:
selected4 = null;
HTML
<select [(ngModel)]="selected4">
<option value="" disabled selected>Select your option</option>
<option value="1">Hey</option>
<option value="2">Hello</option>
</select>
stackblitz

Cannot integrate a place holder in a dropdown menu when using ngModel

I have a dropdown menu using an *ngFor loop and all the options are data bound. I'm trying to put a placeholder in until one option is selected and if I use the first set of code the placeholder appears perfectly but when I include the [(ngModel)] (which I need) the placeholder disappears.
Note: The ngModel is bound to the index of the dropdown so I can use that to filter a JSON file, which is why I also have an index running.
I've tried using 'placeholder' and 'required placeholder' and adding an initial option in before the for loop options
HTML Code with working placeholder but no ngModel
<label for="filter-coach-sel">Option</label>
<select required placeholder="" class="form-control" id="filter-coach-sel" >
<option hidden value="" disabled selected>Select an option </option>
<option *ngFor = " let coach of navHistory;">{{coach.account.name}}</option>
</select>
HTML Code with ngModel but placeholder no longer works
<div class="form-group">
<label for="filter-coach-sel">Coach</label>
<select required placeholder="" [(ngModel)]="selectedCoachIndex" (change) = "changeCoach()" class="form-control" id="filter-coach-sel" >
<option disabled selected>Select one category </option>
<option *ngFor = " let coach of navHistory; let i = index" value="{{i}}"> {{coach.account.first_name}}</option>
</select>
</div>
Since you are using ngModel to bind the select box, your placeholder option also needs to be one among the value. Try initializing as selectedCoachIndex=""; as assigning value="" for your option
<div class="form-group">
<label for="filter-coach-sel">Coach</label>
<select [(ngModel)]="selectedCoachIndex" class="form-control" >
<option disabled selected value="">Select one category </option>
<option *ngFor = " let coach of navHistory; let i = index" value="{{i}}"> {{coach.first_name}}</option>
</select>
</div>
And your .ts file would have something like this
navHistory = [{
first_name: 'Bob'
},{
first_name: 'Jon'
},{
first_name: 'Mat'
}];
selectedCoachIndex = "";
Stackblitz Demo

Using [(ngModel)] and having an empty select element

I am using [(ngModel)] top bind various inputs and select elements in my angular application. I also use a service with ngModel. When using the select element, my app is showing an empty option.
Even if I change the value of the first option to something other than an empty string, the same issue comes up.
<div>
<input
placeholder="Ohio Volume"
type="number"
class="text-input"
value="{{ this.inputsService.ohioVolume }}"
[(ngModel)]="ohioVolume"
(change)="this.inputsService.setOhioVolume(ohioVolume)"
>
</div>
<div>
<select
class="select"
value="{{ this.inputsService.ohioReporter }}"
[(ngModel)]="ohioReporter"
(change)="this.inputsService.setOhioReporter(ohioReporter)">
<option value="">Ohio Reporter</option>
<option value="Ohio St.3d">Ohio St.3d</option>
<option value="Ohio St.2d">Ohio St.2d</option>
<option value="Ohio St.">Ohio St.</option>
</select>
</div>
My inputsService typescript file shows in relevant part:
ohioReporter: string
setOhioReporter(ohioReporter) {
this.ohioReporter = ohioReporter
}
The input code is working appropriately, but the select shows an empty box regardless of the value of the first option. I would prefer the solution to remain only in the html code and not require any typescript code.
Try with this in your ts/Service, maybe its because its not defined by default
ohioReporter: string = ""
you need to modify yout code in this way:
<select
class="select"
value="{{ this.inputsService.ohioReporter }}"
formControlName="name" [ngModel]="ohioReporter" name="ohioReporter" (ngModelChange)="ohioReporter($event)">
<option value="">Ohio Reporter</option>
<option value="Ohio St.3d">Ohio St.3d</option>
<option value="Ohio St.2d">Ohio St.2d</option>
<option value="Ohio St.">Ohio St.</option>
</select>
the .ts becames:
#Output() ohioReporter: number;
....
ohioReporter(ohioReporter) {
this.ohioReporter= ohioReporter;
}

static angular select option

<input [formControl]="twitterHandle" id="twitterHandle" placeholder="twitterHandle">
in there i get input via that and using following code get the input value
twitterHandle=new FormControl();
twitterHandle:this.twitterHandle.value,
simillar way i need to add static select input to form how to do that and how to edit following
<select>
<option value="option">country1</option>
<option value="option1">Sri lanka</option>
<option value="option2">Canada</option>
</select>
You just have to define another formControl :
selectControl : FormControl = new FormControl();
and you can initialize the control :
ngOnInit(){
this.selectControl.setValue('option')
}
In your template ,
<form>
<select [formControl]='selectControl' (change)='test()'>
<option [value]='"option"'>Option1</option>
<option [value]='"option1"'>Option2</option>
</select>
</form>
you can check the value changes in the test() as defined below :
test(){
console.log(this.selectControl.value);
}
You can See the example : https://angular-wytvwr.stackblitz.io
Hope this helps

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