Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give placeholder to select in Angular 6

I have a select option in which i want to give placeholder which says "select a category"

<form role="form" class="form form-horizontal" (ngSubmit)="onSubmit()" #form="ngForm" ngNativeValidate>
        <div class="form-group row">
            <div class="col-xl-4 col-lg-6 col-md-12">
                <fieldset class="form-group">
                    <label for="customSelect">Categories:</label>
                    <select class="custom-select d-block w-100" id="Category" [(ngModel)]="Category" name="Category" required>
                        <option value=" ">Select one category </option>                                     
                         <option *ngFor="let item of myBusinessList" [value]="item.id">{{item.name}}</option>
                    </select>
                </fieldset>                                
            </div>
        </div>
        <button type="submit" class="btn btn-raised btn-danger">Save</button>
    </form>

If i remove [ngModel] then it works. If i write

<option value="undefined" selected>Select one category </option>    

then it considers as one of the value. I have to make sure there is place and also it is required to select one of the value

like image 233
Ruchita Ajmera Avatar asked Aug 21 '18 12:08

Ruchita Ajmera


People also ask

How do I create a placeholder dropdown?

There is no attribute like input's placeholder for select box dropdown. However, you can create similar effect by using the HTML disabled and selected attribute on a <option> element that has empty value.

How do you select placeholder style?

The ::placeholder selector selects form elements with placeholder text, and let you style the placeholder text. The placeholder text is set with the placeholder attribute, which specifies a hint that describes the expected value of an input field.

What is placeholder in angular?

Placeholders can be used to enhance the experience of your application. You will, need set some custom widths to toggle their visibility. Their appearance, color, and sizing can be easily customized with our utility classes.


Video Answer


2 Answers

If you want the first value to be selected when Category is still undefined, you can assign the value undefined to the first option with ngValue:

<option [ngValue]="undefined" hidden>Select one category</option>
like image 158
ConnorsFan Avatar answered Oct 21 '22 04:10

ConnorsFan


You can use [value]="" selected hidden

I have create a demo on Stackblitz

<form role="form" class="form form-horizontal" (ngSubmit)="onSubmit()" #form="ngForm" ngNativeValidate>
    <div class="form-group row">
        <div class="col-xl-4 col-lg-6 col-md-12">
            <fieldset class="form-group">
                <label for="customSelect">Categories:</label>
                <select class="custom-select d-block w-100" id="Category" [(ngModel)]="Category" name="Category" required placeholder="d.ff">
                    <option hidden [value]=""  selected>Select one category </option>
                    <option *ngFor="let item of myBusinessList" [value]="item.id">{{item.name}}</option>
                </select>
            </fieldset>
        </div>
    </div>
    <button type="submit" class="btn btn-raised btn-danger">Save</button>
</form>
like image 41
Krishna Rathore Avatar answered Oct 21 '22 03:10

Krishna Rathore