swetha m
swetha m

Reputation: 51

AngularJS - How to add placeholder text to dropdown

I am new to angularjs. I want to add a placeholder to my dropdown. I am not hard coding my dropdown values, it's coming from some where. Can you tell me where i did mistake.

<div class="row form-group">
    <label class="col-md-3">Action<span style="color: #b94a48">*</span></b></label>
    <div class="col-lg-3 col-md-7" style="padding: 0px;">
        <div class="dropdown select" style="width: 100%;">
            <select name="wlOrdActType" class="dropdown multiple" placeholder="-- Please Select --" 
                ng-model="wlOrdActType" 
                ng-options="obj.name as obj.name for obj in ordertypeList"
                style="width: 100%;">
                    <option></option>
            </select>
        </div>
    </div>
</div>

Here every thing is working. But placeholder is not place on the dropdown

Upvotes: 5

Views: 29922

Answers (4)

I am using Angular v1.6.6. So, check for older version's compatibility. I believe the below code should work v1.5 onwards.

$scope.ordertypeList = ordertypeListFromApiCall; // Get the collection from API
$scope.ordertypeList.splice(0,0, {'id' : '', 'name' : 'Select Order Type...' } );                 
$scope.wlOrdActType = $scope.ordertypeList[0];

And in the HTML,

<select class="form-control" ng-model="wlOrdActType" required ng-options="ordertype.name disable when ordertype.id == '' for ordertype in ordertypeList track by ordertype.id">
</select>

In the itemSelected function, make sure that the id of $scope.wlOrdActType is not empty

Upvotes: 0

Peter Buju
Peter Buju

Reputation: 187

Could also try this:

<select required class="form-control" ng-model="myModel" 
ng-options="item as item.title for item in list">
    <option value="" label="-- Select option --" disabled selected="selected">      
    </option>
</select>

While having the model set to an empty string in the controller.

 $scope.myModel = "";

Upvotes: 3

MatayoshiMariano
MatayoshiMariano

Reputation: 2106

You can do something like this:

<select ng-model="wlOrdActType" ng-options="obj.name as obj.name for obj in ordertypeList">
  <option value="" selected="selected">Choose one</option>
</select>

Upvotes: 6

binary1101
binary1101

Reputation: 179

<option value="" disabled selected>Please Select</option>

Upvotes: 17

Related Questions